Found 6702 Articles for Database

Sort values that contain letters and symbols in custom order with MySQL

AmitDiwan
Updated on 30-Sep-2019 07:25:28

132 Views

For custom order, use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable (    Title varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Java_1+'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('MySQL_23+'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('MongoDB++'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('C++_23'); 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 ... Read More

MySQL query to display records on the basis of conditions IS NULL OR !=1;?

AmitDiwan
Updated on 30-Sep-2019 07:23:28

79 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100),    Score int ); Query OK, 0 rows affected (1.10 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Score) values('John', 45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('Chris', null); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('David', null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name, Score) values('Bob', 1); Query OK, 1 row affected (0.11 sec)Display all records from ... Read More

Fetch datetime row from exactly past 7 days records in MySQL

AmitDiwan
Updated on 30-Sep-2019 07:15:19

559 Views

For this, you can use the INTERVAL 7 day concept. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    AdmissionDate datetime ); Query OK, 0 rows affected (0.83 sec)Note − Let’s say the current date is 2019-08-23.Insert some records in the table using insert command −mysql> insert into DemoTable(AdmissionDate) values('2019-01-23'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-15'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-16'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-24'); Query OK, 1 ... Read More

Fetching records from a table with NULL and other values on the basis of conditions in MySQL

AmitDiwan
Updated on 30-Sep-2019 07:12:36

91 Views

Let us first create a table −mysql> create table DemoTable (    value1 int,    value2 int,    value3 int ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20, 40, null); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(40, 40, null); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(null, null, null); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+--------+--------+ | value1 | value2 ... Read More

MySQL query to ORDER BY records on the basis of modulus result

AmitDiwan
Updated on 30-Sep-2019 07:08:44

99 Views

For this, use ORDER BY with a modulus operator. Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentName varchar(100) ); Query OK, 0 rows affected (1.88 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(101, 'Robert'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(103, 'Mike'); Query OK, 1 row affected (0.23 sec)Display all records from the table using ... Read More

MySQL multiple COUNT with multiple columns?

AmitDiwan
Updated on 30-Sep-2019 07:05:35

301 Views

You can use an aggregate function SUM() along with IF(). Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (2.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('John', 'Smith'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (1.38 sec) mysql> insert into DemoTable values('Bob', 'Doe'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Sam', ... Read More

Search text containing a new line in MySQL?

AmitDiwan
Updated on 06-Jul-2020 06:00:18

565 Views

You can use REGEXP. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (1.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('JohnSmith'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('John Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('DavidMiller'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.27 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

Use LIKE % to fetch multiple values in a single MySQL query

AmitDiwan
Updated on 30-Sep-2019 07:00:04

3K+ Views

To fetch multiple values wit LIKE, use the LIKE operator along with OR operator. Let us first create a table −mysql> create table DemoTable1027 (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (1.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1027 values(100, 'John'); Query OK, 1 row affected (0.72 sec) mysql> insert into DemoTable1027 values(20, 'Chris'); Query OK, 1 row affected (0.56 sec) mysql> insert into DemoTable1027 values(200, 'Robert'); Query OK, 1 row affected (0.84 sec) mysql> insert into DemoTable1027 values(400, 'Mike'); Query OK, 1 row affected (0.47 sec)Display all ... Read More

MySQL select query to fetch data with null value?

AmitDiwan
Updated on 30-Sep-2019 06:57:06

398 Views

Let us first create a table −mysql> create table DemoTable (    CustomerName varchar(100),    CustomerCountryName varchar(100) ); Query OK, 0 rows affected (0.95 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 'US'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Bob', 'UK'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Carol', NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David', 'AUS'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Mike', NULL); Query OK, 1 row affected (0.18 sec)Display all records ... Read More

Show column value twice in MySQL Select?

AmitDiwan
Updated on 30-Sep-2019 06:54:42

213 Views

You can use concat(). Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(100); Query ... Read More

Advertisements