Kumar Varma has Published 107 Articles

Limiting numbers to a maximum value in MySQL?

Kumar Varma

Kumar Varma

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

201 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

394 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

227 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

255 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

171 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

406 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

Display records with more than two occurrences in MySQL?

Kumar Varma

Kumar Varma

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

746 Views

For this, you can use GROUP BY HAVING clause. 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.53 sec)Insert some records in the table ... Read More

MySQL query to select a count on two separate conditions?

Kumar Varma

Kumar Varma

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

246 Views

Use CASE statement for this. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentMarks int,    -> isValid tinyint(1)    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(45, 0); ... Read More

How to SELECT fields from one table and INSERT to another in MySQL?

Kumar Varma

Kumar Varma

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

197 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> StudentId int,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10, 'John'); Query OK, 1 row affected ... Read More

Advertisements