Found 4378 Articles for MySQL

How to get age from DOB in MySQL?

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

302 Views

To get age from DOB, you can use the TIMESTAMPDIFF() function. Following is the syntax −select TIMESTAMPDIFF(YEAR, yourColumnName, CURRENT_DATE) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> DateOfBirth datetime    -> ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(DateOfBirth) values('1998-06-04 12:30:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(DateOfBirth) values('2000-01-31 04:10:20'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(DateOfBirth) values('2010-12-01 03:50:45'); ... Read More

How to set a comma separated list as a table in MySQL?

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

373 Views

You can use UNION ALL for this.Let us get list 10, 20, 30, 40, 50 as a table with UNION ALL −mysql> select 10 Number UNION ALL select 20 Number UNION ALL select 30 Number     UNION ALL select 40 Number UNION ALL select 50 Number;Output+--------+ | Number | +--------+ | 10 | | 20 | | 30 | | 40 | | 50 | +--------+ 5 rows in set (0.00 sec)Let us see another example. To get the list 1,2,3 as a table, use the below query −mysql> SELECT 1 a UNION ALL SELECT 2 a UNION ALL SELECT 3 a;Output+---+ | a | +---+ | 1 | | 2 | | 3 | +---+ 3 rows in set (0.00 sec)

Limiting numbers to a maximum value in MySQL?

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

118 Views

For this, you can use LEAST(). Following is the syntax −select least(yourColumnName, yourMaxValue) from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> Number int    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(201); Query OK, 1 row affected (0.12 sec) ... Read More

MySQL query to select rows older than a week?

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

232 Views

For this, you can use DATEDIFF() function. The current date time is as follows −mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 19:15:56 | +---------------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable -> ( -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.19 sec) ... Read More

MySQL query to count where more than three columns values are true?

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

89 Views

To count where more than three column values are true, you can use WHERE clause. Let us first create a table −mysql> create table DemoTable    -> (    -> isMarried boolean,    -> isActive boolean,    -> isMember boolean,    -> isOn boolean    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(true, false, true, false); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(false, false, false, false); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(true, true, ... Read More

MySQL query to select records beginning from a specific id

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

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name) ... Read More

Set AUTO_INCREMENT in a table while creating it in MySQL?

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

128 Views

Let us first create a table. We have used AUTO_INCREMENT while creating the table to set auto increment for StudentId −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> StudentFirstName varchar(100),    -> StudentLastName varchar(100),    -> StudentAge int,    -> StudentCountryName varchar(100),    -> PRIMARY KEY(StudentId)    -> )AUTO_INCREMENT=30; Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentFirstName, StudentLastName, StudentAge, StudentCountryName) values('John', 'Smith', 21, 'US'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFirstName, StudentLastName, StudentAge, StudentCountryName) values('Chris', 'Brown', ... Read More

How to sort multiple columns with a single query?

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

193 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

407 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

684 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

Advertisements