Found 4378 Articles for MySQL

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

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 (0.20 sec) mysql> insert into DemoTable values('86'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('45'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('85'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('085'); Query OK, 1 row affected (0.14 sec) ... Read More

Converting boolean values to positive or negative sign in MySQL?

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

305 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 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.36 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will ... Read More

Set a custom value for NULL or empty values in MySQL

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

616 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 row affected (0.09 sec) mysql> insert into DemoTable values('200'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | +-------+ ... Read More

Find the number of logins between two dates in MySQL

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

115 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 sec) mysql> insert into DemoTable values('2019-08-12'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-08-20'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-08-24'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will ... Read More

Display multiple selected rows using MySQL IN()

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

452 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) mysql> insert into DemoTable1045(FirstName) values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1045(FirstName) values('Robert'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1045(FirstName) values('Mike'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1045(FirstName) values('Adam'); Query OK, 1 row affected (0.08 sec)Display all records ... Read More

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

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> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Jace'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into ... Read More

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

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

353 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 records in the table using insert command −mysql> insert into DemoTable(ProductName, ProductAmount) values('Product-1', 60); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ProductName, ProductAmount) values('Product-2', 40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(ProductName, ProductAmount) values('Product-3', 75); Query OK, 1 row affected (0.15 sec) mysql> ... Read More

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

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

244 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> insert into DemoTable values('Adam', '2019-08-24 11:10:00'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam', '2019-08-24 11:20:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Adam', '2019-08-24 11:50:00'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> ... Read More

How to get multiple rows in a single MySQL query?

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

572 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 values(110, 'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(120, 'Robert'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(130, 'Mike'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

Can we use WHERE clause inside MySQL CASE statement?

AmitDiwan
Updated on 30-Sep-2019 07:46:16

210 Views

For this, use the CASE WHEN statement. Let us first create a table −mysql> create table DemoTable1040 (    Value1 int,    Value2 int,    Value3 int ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1040 values(10, 30, 1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1040 values(40, 20, 50); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1040 values(80, 90, 100); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable1040;This will produce the ... Read More

Advertisements