Found 4378 Articles for MySQL

MySQL SELECT from two tables with a single query

AmitDiwan
Updated on 09-Oct-2019 11:49:47

2K+ Views

Use UNION to select from two tables. Let us first create a table −mysql> create table DemoTable1 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20) ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(FirstName) values('Chris') ; Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1(FirstName) values('Adam'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1(FirstName) values('Sam'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+----+-----------+ ... Read More

Get row data for the lowest and highest values in a MySQL column

AmitDiwan
Updated on 09-Oct-2019 11:45:53

412 Views

For the lowest values in a MySQL column, use the MIN() method and for highest, use the MAX() method. Let us first create a table −mysql> create table DemoTable (    CustomerName varchar(20),    ProductAmount int ) ; Query OK, 0 rows affected (1.03 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 3599); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David', 7843); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Mike', 97474); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Bob', 65884); Query OK, ... Read More

How can I avoid too many OR statements in a MySQL query?

AmitDiwan
Updated on 09-Oct-2019 11:40:36

192 Views

Use MySQL IN() to avoid too many OR statements. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40) ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name) values('Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Name) values('David'); ... Read More

How to select different values from same column and display them in different columns with MySQL?

AmitDiwan
Updated on 09-Oct-2019 11:38:11

2K+ Views

To select different values on the basis of condition, use CASE statement. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40), Score int ) ; Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Score) values('Chris', 45); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(Name, Score) values('David', 68); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('Robert', 89); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name, Score) ... Read More

Randomly SELECT distinct rows in a MySQL table?

AmitDiwan
Updated on 09-Oct-2019 11:32:15

594 Views

To randomly select rows, use ORDER BY RAND() with LIMIT. Use DISTINCT for distinct rows. Let us first see an example and create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40) ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John Doe'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name) values('Chris Brown'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name) values('Adam Smith'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Name) ... Read More

Update a MySQL table with Java MySQL

AmitDiwan
Updated on 14-Feb-2020 10:24:53

3K+ Views

For this, you need to use PreparedStatement in Java for update. Let us first create a table −mysql> create table DemoTable(    Id int,    FirstName varchar(40) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(111, 'Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(121, 'Sam'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce the following output ... Read More

Use delimiter correctly in a MySQL stored procedure to avoid BEGIN/END statements errors

AmitDiwan
Updated on 09-Oct-2019 11:20:01

102 Views

Such errors arise when you avoid using the DELIMITER concept. Let us see an example and run a query for stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE correct_procedure()    BEGIN    SELECT 'Hello MySQL !!!';    END // Query OK, 0 rows affected (0.12 sec) mysql> DELIMITER ;Following is the syntax to call the stored procedure −call yourStoredProcedureName();Call the stored procedure using CALL command −mysql> call correct_procedure();This will produce the following output −+-----------------+ | Hello MySQL !!! | +-----------------+ | Hello MySQL !!! | +-----------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.02 sec)

Set new delay time in a MySQL column

AmitDiwan
Updated on 09-Oct-2019 11:18:35

173 Views

To set new delay time, use INTERVAL and update the column wth SETa clause and UPDATE command. Let us first create a table −mysql> create table DemoTable (    DelayTime time ); Query OK, 0 rows affected (1.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('11 :30 :10'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('12 :40 :00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('05 :45 :24'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('09 :00 :10'); Query OK, 1 row ... Read More

MySQL RegEx to find lines containing N semi-colons?

AmitDiwan
Updated on 09-Oct-2019 11:15:40

176 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY ,    Title text ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Title) values('This is; a; MySQL;Tutorial'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Title) values('Java is; an;Object Oriented'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Title) values('MongoDB ; is;a; database'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

How to find tables with a specific column name in MySQL?

AmitDiwan
Updated on 09-Oct-2019 11:12:56

982 Views

To find column names, use information_schema.columns. Following is the syntax −select distinct table_name from information_schema.columns where column_name like '%yourSearchValue%' and table_schema=database();Let us implement the above syntax in order to find column names across various table. Here, we want only table names with a specific column name word “Client” −mysql> select distinct table_name    from information_schema.columns    where column_name like '%Client%'    and table_schema=database();This will produce the following output −+----------------+ | table_name    | +----------------+ | demotable449 | | demotable450 | | demotable461 | | demotable517 | | demotable529 | ... Read More

Advertisements