Found 4381 Articles for MySQL

Match optional end of line after every record with REGEXP?

AmitDiwan
Updated on 22-Aug-2019 08:00:26

79 Views

Let us first create a table −mysql> create table DemoTable(EmployeeCode varchar(100)); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('EMPLOYEE:100 John Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('EMPLOYEE:16537 Chris Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('EMPLOYEE:100 David Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('EMPLOYEE:100 23432 David Miller'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql>; select *from DemoTable;This will produce the following output ... Read More

How to merge rows in MySQL?

AmitDiwan
Updated on 22-Aug-2019 07:49:01

5K+ Views

To merge rows in MySQL, use GROUP_CONCAT().Let us first create a table−mysql> create table DemoTable734 (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command−mysql> insert into DemoTable734 values(101, 'John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable734 values(102, 'John'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable734 values(103, 'Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable734 values(104, 'Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable734 values(104, 'Chris'); Query OK, 1 row affected ... Read More

Search records on the basis of date in MySQL?

AmitDiwan
Updated on 22-Aug-2019 07:45:58

163 Views

Let us first create a table -mysql> create table DemoTable732 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, PassengerId int, PassengerName varchar(100), PassengerAge int, PassengerTravelDatetime datetime ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command -mysql> insert into DemoTable732(PassengerId, PassengerName, PassengerAge, PassengerTravelDatetime) values(110, 'Chris', 25, '2019-07-23 12:45:56'); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable732(PassengerId, PassengerName, PassengerAge, PassengerTravelDatetime) values(120, 'Robert', 24, '2019-07-21 11:05:00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable732(PassengerId, PassengerName, ... Read More

Delete multiple entries from a MySQL table

AmitDiwan
Updated on 22-Aug-2019 07:52:05

800 Views

To delete multiple entries from a MySQL table, use JOIN. Let us first create a table −mysql> create table DemoTabl(Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100)); Query OK, 0 rows affected (0.69 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('Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(FirstName) values('Bob'); Query OK, 1 row ... Read More

Convert string (varchar) to double in MySQL

AmitDiwan
Updated on 22-Aug-2019 07:43:51

2K+ Views

Let us first create a table:mysql> create table DemoTable731 (Value varchar(100)); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command -mysql> insert into DemoTable731 values('4.50'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable731 values('7.83'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable731 values('8.91'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable731 values('560.34'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement -mysql> select *from DemoTable731;This will produce the following output -+--------+ | Value | +--------+ | 4.50 ... Read More

MySQL query to count days in date range with start and end date

AmitDiwan
Updated on 22-Aug-2019 07:41:11

855 Views

To count days in date range, you need to find the difference between dates using DATEDIFF().Let us first create a table:mysql> create table DemoTable730 (    StartDate date,    EndDate date ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command:mysql> insert into DemoTable730 values('2019-01-21', '2019-07-21'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable730 values('2018-10-11', '2018-12-31'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable730 values('2016-01-01', '2016-12-31'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement:mysql> select *from DemoTable730;This will produce the following ... Read More

Assign an SQL result to variable from prepared statement in MySQL?

AmitDiwan
Updated on 22-Aug-2019 07:42:59

2K+ Views

For this, use stored procedure. Let us first create a table −mysql> create table DemoTable(Id int, Name varchar(100)); Query OK, 0 rows affected (1.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(11, 'Chris'); Query OK, 1 row affected (0.41 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------+-------+ | Id | Name    | +------+-------+ | 10 | John    | | 11 | Chris | +------+-------+ 2 rows ... Read More

Display the student marks in a single column on the basis of subject in MySQL?

AmitDiwan
Updated on 22-Aug-2019 07:40:10

2K+ Views

For this, use UNION ALL.Let us first create a table:mysql> create table DemoTable729 (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100),    MySQLMarks int,    CMarks int,    JavaMarks int ); Query OK, 0 rows affected (0.40 sec)Insert some records in the table using insert command:mysql> insert into DemoTable729(StudentName, MySQLMarks, CMarks, JavaMarks) values('Chris', 94, 67, 75); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable729(StudentName, MySQLMarks, CMarks, JavaMarks) values('Robert', 45, 99, 54); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable729(StudentName, MySQLMarks, CMarks, JavaMarks) values('David', 57, 89, 43); Query OK, 1 row ... Read More

Convert UK DATE to MySQL date?

AmitDiwan
Updated on 22-Aug-2019 07:36:41

402 Views

The UK date format supports day-moth-year format. To convert it to MySQL date, use STR_TO_DATE(). Following is the syntax:select str_to_date(yourColumnName, '%d/%m/%Y') from yourTableName;Let us first create a table:mysql> create table DemoTable728 (DueDate varchar(100)); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command:mysql> insert into DemoTable728 values('10/11/2019'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable728 values('31/01/2016'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable728 values('01/12/2015'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable728 values('11/03/2018'); Query OK, 1 row affected (0.16 sec)Display all records from the table ... Read More

Selecting a value in custom order from another column in a MySQL table with a single query

AmitDiwan
Updated on 22-Aug-2019 07:35:16

145 Views

For this, you can use IN().Let us first create a table:mysql> create table DemoTable727 (    Name varchar(100),    Score int ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command:mysql> insert into DemoTable727 values('Chris', 45); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable727 values('Robert', 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable727 values('Carol', 94); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable727 values('David', 93); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable727 values('Mike', 78); Query OK, 1 row affected (0.21 ... Read More

Advertisements