Database Articles - Page 222 of 618

Find rows with a match in a pipe delimited column with MySQL

AmitDiwan
Updated on 13-Dec-2019 06:14:20

559 Views

To find a match, use regular expressions in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Value varchar(60)    -> ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('8|56|78|45'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('9876'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('98|8'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('3|8|9'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('97|94'); Query OK, 1 ... Read More

How to add a string containing double quote to records in MySQL?

AmitDiwan
Updated on 13-Dec-2019 06:11:43

565 Views

To insert records with double quotes, use a backslash (\) as in the below syntax −Syntaxinsert into yourTableName values('\"yourValue\"');Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('\"John\"'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('\"Chris\"'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('\"Adam Smith\"'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('\"Carol\"'); Query OK, 1 row affected ... Read More

MySQL search and replace record from a list of records

AmitDiwan
Updated on 13-Dec-2019 06:10:10

155 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> ListOfName text    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Carol, Sam, John, David, Bob, Mike, Robert, John, Chris, James, Jace'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------------------------------------------------------+ | ListOfName                                               ... Read More

How to extract date from string in MySQL?

AmitDiwan
Updated on 13-Dec-2019 06:06:20

762 Views

To extract date from the string in MySQL, use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable    -> (    -> Title text    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John has got joining date.12/31/2018'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Carol has got joining date.01/11/2019'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Sam will arrive at.12/03/2050'); Query OK, 1 row affected (0.87 sec)Display all records from the table using select statement ... Read More

Mobile

MySQL query to find the number of occurrences from two columns?

AmitDiwan
Updated on 13-Dec-2019 06:03:54

509 Views

Use MySQL GROUP_BY to find the number of occurrences from two columns. Let us first create a table −mysql> create table DemoTable    -> (    -> Name1 varchar(20),    -> Name2 varchar(20)    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 'Adam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris', 'David'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Robert', 'Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('David', 'Chris'); Query OK, 1 row ... Read More

Find the difference between dates in the form of months with MySQL

AmitDiwan
Updated on 13-Dec-2019 06:01:00

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Date1 date,    -> Date2 date    -> ); Query OK, 0 rows affected (1.04 sec)Insert some records in the table using insert command &miuns;mysql> insert into DemoTable values('2017-01-10', '2017-12-10'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2018-12-31', '2015-01-02'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2020-03-01', '2019-06-15'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------+------------+ | Date1     ... Read More

MySQL select * and find record with current date

AmitDiwan
Updated on 13-Dec-2019 05:59:12

3K+ Views

For the current date, use CURDATE(). Also, use STR_TO_DATE() to format date and compare it with the current date as in the below syntax −Syntaxselect *from yourTableName where str_to_date(yourColumnName, 'yourFormatSpecifier')=curdate();Let’s say the current date is 27/10/2019.Let us first create a table −mysql> create table DemoTable    -> (    -> JoiningDate varchar(40)    -> ); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('27/10/2017'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('27/10/2018'); Query OK, 1 row affected (0.78 sec) mysql> insert into DemoTable values('27/10/2019'); Query ... Read More

How to use ORDER BY field and sort by id in a single MySQL field?

AmitDiwan
Updated on 13-Dec-2019 05:56:42

695 Views

For this, you can use ORDER BY FIELD. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values(201, 'Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(110, 'Adam'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable values(250, 'John'); Query OK, 1 row affected (0.33 sec)Display all ... Read More

Select distinct values from three columns and display in a single column with MySQL

AmitDiwan
Updated on 13-Dec-2019 05:54:03

575 Views

For this, use UNION more than once in a single MySQL query. Let us first create a table −mysql> create table DemoTable    -> (    -> Value1 int,    -> Value2 int,    -> Value3 int    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20, null, null); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(20, null, null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(20, null, null); Query OK, 1 row affected (0.12 sec) mysql> insert into ... Read More

Implement MySQL CASE statement with WHEN clause

AmitDiwan
Updated on 13-Dec-2019 05:51:24

246 Views

CASE statement with the WHEN clause is used to work around conditions. Following is the syntax−select *,    case when yourCondition then yourStatement    when yourCondition then yourStatement    .    . else yourStatement from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> StudentName varchar(20),    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 78); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert', 88); Query OK, 1 row affected (0.14 sec) ... Read More

Advertisements