AmitDiwan has Published 11365 Articles

Display record with today and tomorrow’s date from a column with date record in MySQL

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 08:07:19

434 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-08-24'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-08-25'); Query OK, 1 ... Read More

How do I sort numbers saved as VARCHAR in MySQL with some of them having preceding 0 like 085, 090, etc.?

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 08:05:29

24 Views

Following is the syntax −select *from yourTableName order by yourColumnName*1, yourColumnName;Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('90'); Query OK, 1 row affected ... Read More

Converting boolean values to positive or negative sign in MySQL?

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 08:03:55

301 Views

Following is the syntax −select if(yourColumnName, 1, -1) from yourTableName;Let us first create a table −mysql> create table DemoTable (    isMarried boolean ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.21 ... Read More

Set a custom value for NULL or empty values in MySQL

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 08:00:43

610 Views

Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('100'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(''); Query OK, 1 ... Read More

Find the number of logins between two dates in MySQL

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 07:59:03

111 Views

Use BETWEEN to find the logins between two dates. Let us first create a table −mysql> create table DemoTable (    Login datetime ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-08-10'); Query OK, 1 row affected (0.11 ... Read More

Display multiple selected rows using MySQL IN()

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 07:57:35

451 Views

Let us first create a table −mysql> create table DemoTable1045 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) )AUTO_INCREMENT=100; Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1045(FirstName) values('John'); Query OK, 1 row affected (0.15 sec) ... Read More

Advanced sorting in MySQL to display strings beginning with J at the end even after ORDER BY

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 07:55:12

104 Views

For this, use ORDER BY LIKE operator. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (0.60 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> ... Read More

Display all products having the highest amount in a MySQL table with product details?

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 07:53:23

346 Views

Use MAX() along with subquery for this Here, MAX() is used to get the maximum amount. Let us first create a table −mysql> create table DemoTable (    ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ProductName varchar(100),    ProductAmount int ); Query OK, 0 rows affected (0.53 sec)Insert some ... Read More

How to compare the first date and the last date from a MySQL column with date records?

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 07:49:45

243 Views

To compare the first date with the last date, use TIME_TO_SEC() along with MAX() and MIN(). Let us first create a table −mysql> create table DemoTable (    UserName varchar(100),    UserPostDatetime datetime ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> ... Read More

How to get multiple rows in a single MySQL query?

AmitDiwan

AmitDiwan

Updated on 30-Sep-2019 07:48:08

566 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable ... Read More

Advertisements