Found 4378 Articles for MySQL

MySQL CONCAT a specific column value with the corresponding record

AmitDiwan
Updated on 26-Sep-2019 07:43:56

85 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    CountryName varchar(100) ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 'US'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David', 'AUS'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris', 'UK'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('John', 'US'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Carol', 'AUS'); Query OK, 1 row affected (0.14 sec)Display all records ... Read More

INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table

AmitDiwan
Updated on 26-Sep-2019 07:41:48

916 Views

Here’s the syntax to check whether the table already exists using the CREATE TABLE IF NOT EXISTS −create table if not exists yourTableName (    yourColumnName1 dataType,    .    .    .    .    N ); insert into yourTableName values(yourValue1, .......N);Let us first create a table −mysql> create table if not exists DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentSubject varchar(100) ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentSubject) values('Java'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(StudentSubject) values('MySQL'); ... Read More

How to find a specific record from a list of values with semicolon in MySQL?

AmitDiwan
Updated on 26-Sep-2019 07:38:20

97 Views

For this, you can use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value varchar(100) ); Query OK, 0 rows affected (2.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values('100;200;300'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable(Value) values('1;300;400'); Query OK, 1 row affected (0.58 sec) mysql> insert into DemoTable(Value) values('6;7;8;9;10'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable(Value) values('1;2;3;4;5'); Query OK, 1 row affected (9.36 sec) mysql> insert into DemoTable(Value) values('3;8;9;10'); Query OK, 1 ... Read More

MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?

AmitDiwan
Updated on 26-Sep-2019 07:36:06

1K+ Views

Let us first create a table &mnus;mysql> create table DemoTable (    `timestamp` timestamp ); Query OK, 0 rows affected (1.12 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(now()); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('00:00:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-01-10 12:34:45'); Query OK, 1 row affected (0.80 sec) mysql> insert into DemoTable values('2019-12-31 10:50:45'); Query OK, 1 row affected (0.84 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------------------+ | timestamp ... Read More

MySQL group_concat to add a separator for empty fields?

AmitDiwan
Updated on 26-Sep-2019 07:33:08

342 Views

For this, you can use replace() along with group_concat(). Here, for empty fields, we are displaying a comma as a separator. Let us first create a table −mysql> create table DemoTable (    Id int,    Number varchar(100) ); Query OK, 0 rows affected (2.03 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, '456'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(11, '345'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(10, ''); Query OK, 1 row affected (0.63 sec) mysql> insert into DemoTable values(10, '278'); Query ... Read More

How to ORDER BY DESC and display the first 3 records in MySQL?

AmitDiwan
Updated on 26-Sep-2019 07:29:43

494 Views

For this, you can use ORDER BY DESC with LIMIT. Let us first create a table −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(100) ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(UserName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(UserName) values('Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(UserName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(UserName) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(UserName) ... Read More

MySQL ORDER BY strings with underscore?

AmitDiwan
Updated on 26-Sep-2019 07:27:27

233 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John_Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('John_Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select ... Read More

Insert multiple rows from another table but the inserted records should be distinct

AmitDiwan
Updated on 26-Sep-2019 07:24:07

215 Views

For this, you can use DISTINCT along with the INSERT INTO SELECT statement. Let us first create a table −mysql> create table DemoTable1 (    Value int ); Query OK, 0 rows affected (1.03 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(50); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values(60); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1 values(50); Query OK, 1 row ... Read More

Limit the count using GROUP BY in MySQL

AmitDiwan
Updated on 26-Sep-2019 07:20:43

1K+ Views

Let us first create a table −mysql> create table DemoTable (    UserId int,    UserMessage varchar(100) ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 'Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(2, 'Hello'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(2, 'Good'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(1, 'Nice'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(1, 'Awesome'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

How to get MySQL query result in same order as given by IN clause?

AmitDiwan
Updated on 26-Sep-2019 07:18:18

1K+ Views

For this, you can use IN() along with ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values('Sam'); Query OK, 1 row affected (0.12 sec) mysql> insert into ... Read More

Advertisements