Found 4219 Articles for MySQLi

MySQL query to get next closest day between two days?

AmitDiwan
Updated on 10-Dec-2019 06:15:51

93 Views

Following is the syntax −select * from yourTableName order by ( yourColumnName> now()) desc, (case when yourColumnName > now() then yourColumnName end) ,    yourColumnName  desc limit 1;Let us first create a table −mysql> create table DemoTable1454    -> (    -> ShippingDate date    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1454 values('2019-10-01'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1454 values('2019-10-03'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1454 values('2019-10-05'); Query OK, 1 row affected (0.25 sec) mysql> insert ... Read More

Print structured MySQL SELECT at command prompt

AmitDiwan
Updated on 10-Dec-2019 06:09:40

144 Views

To print, the syntax is as follows −mysql -uroot -t -e "your Select Query  " -pTo implement the above syntax, let us open the command prompt −Now, reach the MySQL bin −Let us implement the above syntax to easily print structured SQL select. Following is the query −This will produce the following output −

Count duplicate ids and display the result in a separate column with MySQL

AmitDiwan
Updated on 10-Dec-2019 06:06:06

144 Views

Let us first create a table −mysql> create table DemoTable1453    -> (    -> CustomerId int,    -> CustomerReviewNumber int    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1453 values(10, 4); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1453 values(10, 4); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1453 values(11, 5); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1453 values(11, 5); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable1453 values(11, 5); Query OK, 1 ... Read More

Count multiple rows and display the result in different columns (and a single row) with MySQL

AmitDiwan
Updated on 10-Dec-2019 06:02:38

603 Views

Let us first create a table −mysql> create table DemoTable1452    -> (    -> FavouriteColor varchar(50)    -> ); Query OK, 0 rows affected (2.42 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1452 values('Red'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1452 values('Blue'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable1452 values('Blue'); ... Read More

How to update a MySQL date type column?

AmitDiwan
Updated on 10-Dec-2019 05:59:19

174 Views

Let us first create a table −mysql> create table DemoTable1451    -> (    -> JoiningDate date    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1451 values('2019-07-21'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable1451 values('2018-01-31'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1451 values('2017-06-01'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select * from DemoTable1451;This will produce the following output −+-------------+ | JoiningDate | +-------------+ | 2019-07-21  | | 2018-01-31  | ... Read More

Display matching repeated date records only once in MySQL

AmitDiwan
Updated on 10-Dec-2019 05:56:12

88 Views

Let’s say we are finding records matching with the current date. Since we want repeated matching records only once, use LIMIT.For example, the current date is −2019-10-02Let us first create a table −mysql> create table DemoTable1450    -> (    -> DueDate date    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1450 values('2019-09-30'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1450 values('2019-10-02'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1450 values('2019-10-02'); Query OK, 1 row affected (0.10 sec) mysql> insert into ... Read More

Return maximum value from records in MySQL

AmitDiwan
Updated on 10-Dec-2019 05:53:12

145 Views

Let us first create a table −mysql> create table DemoTable1449    -> (    -> PlayerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> PlayerScore int    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1449(PlayerScore) values(1040); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1449(PlayerScore) values(1450); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable1449(PlayerScore) values(1890); Query OK, 1 row affected (0.72 sec) mysql> insert into DemoTable1449(PlayerScore) values(1650); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement ... Read More

Check if the current date falls in a given date range using MySQL query

AmitDiwan
Updated on 10-Dec-2019 05:50:53

725 Views

Let us first create a table −mysql> create table DemoTable1448    -> (    -> StartDate date,    -> EndDate date    -> ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1448 values('2019-01-21', '2019-03-22'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1448 values('2019-04-05', '2019-10-10'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1448 values('2019-10-01', '2019-10-29'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1448 values('2018-12-31', '2019-12-31'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL db query to fetch records from comma separate values on the basis of a specific value

AmitDiwan
Updated on 10-Dec-2019 05:47:04

127 Views

For this, you can use REGEXP in MySQL. Let’s say you want the row records wherein any of the comma separated value is 90. For this, use regular expression.Let us first create a table −mysql> create table DemoTable1447    -> (    -> Value varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1447 values('19, 58, 90, 56'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1447 values('56, 89, 99, 100'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1447 values('75, 76, 65, ... Read More

Find the difference between current date and the date records from a MySQL table

AmitDiwan
Updated on 10-Dec-2019 05:44:06

275 Views

To find the difference, use the DATEDIFF() method. Let us first create a table −mysql> create table DemoTable1446    -> (    -> DueDate date    -> ); Query OK, 0 rows affected (1.42 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1446 values('2019-01-21'); Query OK, 1 row affected (0.69 sec) mysql> insert into DemoTable1446 values('2019-02-01'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable1446 values('2019-09-30'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select * from DemoTable1446;This will produce the following output −+------------+ | DueDate ... Read More

Advertisements