Kumar Varma has Published 110 Articles

MySQL query to avoid displaying duplicates values?

Kumar Varma

Kumar Varma

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

389 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,   ... Read More

Set AUTO_INCREMENT in a table while creating it in MySQL?

Kumar Varma

Kumar Varma

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

123 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), ... Read More

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

Kumar Varma

Kumar Varma

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

74 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 ... Read More

Limiting numbers to a maximum value in MySQL?

Kumar Varma

Kumar Varma

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

106 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> ... Read More

How to get age from DOB in MySQL?

Kumar Varma

Kumar Varma

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

274 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    -> ... Read More

MySQL query to order and display difference between dates from the current date

Kumar Varma

Kumar Varma

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

145 Views

For this, use ORDER BY clause. The current date is as follows −mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 21:08:16 | +---------------------+ 1 row in set (0.00 sec)Let us first create a table ... Read More

How to find current size (in memory) of table in MySQL?

Kumar Varma

Kumar Varma

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

163 Views

To get the current size of a table, use the following that will display details about a table including the size −show table status like ‘yourTableName’\GLet us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerName ... Read More

How to sum time in MySQL by converting into seconds?

Kumar Varma

Kumar Varma

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

2K+ Views

To convert time to seconds, use the TIME_TO_SEC() method. Let us first create a table −mysql> create table DemoTable    -> (    -> ArrivalTime time    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('04:10:00'); Query ... Read More

MySQL query that returns a specific string if column is null?

Kumar Varma

Kumar Varma

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

116 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.85 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 ... Read More

How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?

Kumar Varma

Kumar Varma

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

172 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Carol'); Query ... Read More

Advertisements