Found 4381 Articles for MySQL

How to sort multiple columns with a single query?

Rama Giri
Updated on 30-Jul-2019 22:30:26

229 Views

Let us first create a table −mysql> create table DemoTable     -> (     -> Id int,     -> Value int     -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 85885); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(101, 885995474); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100, 895943); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+------+-----------+ | Id   | ... Read More

MySQL query to avoid displaying duplicates values?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

513 Views

For this, you can use GROUP BY and use COUNT to get only non-duplicate values. Following is the syntax −select yourColumnName from yourTableName group by yourColumnName having count(*)=1;Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Subject varchar(100)    -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into ... Read More

How can I set 0 if a query returns a null value in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

833 Views

For this, you can use IFNULL(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Value int    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values(100); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Value) values(140); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Value) values(200); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable(Value) values(450); Query OK, 1 row affected (0.13 ... Read More

How to get the previous day with MySQL CURDATE()?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

324 Views

Let us first get the current date using CURDATE(). The current date is as follows −mysql> select CURDATE(); +------------+ | CURDATE() | +------------+ | 2019-06-09 | +------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ShippingDate date    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command. While inserting, we have used date_sub to get the previous day −mysql> insert into DemoTable(ShippingDate) values(date_sub(CURDATE(), interval 1 day)); Query OK, 1 row ... Read More

GROUP BY a column in another MySQL table

Rama Giri
Updated on 30-Jul-2019 22:30:26

323 Views

For this, you can use CREATE TABLE AS SELECT statement. Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CountryName varchar(20)    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(CountryName) values('US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1(CountryName) values('UK'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1(CountryName) values('AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1(CountryName) values('UK'); Query OK, ... Read More

Return the first n letters of a column in MySQL

Rama Giri
Updated on 30-Jul-2019 22:30:26

159 Views

To return the first n letters, use the LEFT() function. Following is the syntax −select left(yourColumnName, yourValue) from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CourseTitle text    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(CourseTitle) values('Java with Spring and Hibernate framework'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable(CourseTitle) values('Python Web Development'); Query OK, 1 row affected (0.22 sec)Display all records from the table using ... Read More

MySQL query with OR & AND conditions

Kumar Varma
Updated on 30-Jul-2019 22:30:26

181 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Age int    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Age) values('John', 21); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name, Age) values(Null, 20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name, Age) values('David', 23); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name, Age) values('Carol', null); ... Read More

Sum the values in a column in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

357 Views

For this, use the aggregate function SUM(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> € int    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(€) values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(€) values(200); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(€) values(190); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from ... Read More

How to extract substring from a sting starting at a particular position in MySQL?

Kumar Varma
Updated on 30-Jul-2019 22:30:26

254 Views

For this, you can use the mid() function. Following is the syntax −select mid(yourColumnName, yourPositionToStart, yourEndValue) as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> Title text    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('My best framework is Spring and Hibernate'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------------------------------------------+ | Title ... Read More

How to perform string matching in MySQL?

Rama Giri
Updated on 30-Jul-2019 22:30:26

315 Views

For string matching, use LIKE operator. Let us first create a table −mysql> create table DemoTable    -> (    -> MonthName varchar(100)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('JFMA'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('JMA'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('JDN'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('JFOSA'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement ... Read More

Advertisements