AmitDiwan has Published 10744 Articles

MySQL select * and find record with current date

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:59:12

3K+ Views

For the current date, use CURDATE(). Also, use STR_TO_DATE() to format date and compare it with the current date as in the below syntax −Syntaxselect *from yourTableName where str_to_date(yourColumnName, 'yourFormatSpecifier')=curdate();Let’s say the current date is 27/10/2019.Let us first create a table −mysql> create table DemoTable    -> (    -> ... Read More

How to use ORDER BY field and sort by id in a single MySQL field?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:56:42

630 Views

For this, you can use ORDER BY FIELD. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.78 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

Select distinct values from three columns and display in a single column with MySQL

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:54:03

540 Views

For this, use UNION more than once in a single MySQL query. Let us first create a table −mysql> create table DemoTable    -> (    -> Value1 int,    -> Value2 int,    -> Value3 int    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in ... Read More

Implement MySQL CASE statement with WHEN clause

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:51:24

216 Views

CASE statement with the WHEN clause is used to work around conditions. Following is the syntax−select *,    case when yourCondition then yourStatement    when yourCondition then yourStatement    .    . else yourStatement from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> ... Read More

Which technique is more efficient for replacing duplicate records in MySQL?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:48:07

107 Views

To replace duplicate records and avoid any error while inserting, use INSERT ON DUPLICATE KEY UPDATE. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20),    -> UNIQUE(Id, Name)    -> ); Query OK, 0 rows affected (0.78 ... Read More

In MySQL, is there a way to turn column records into a list?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:44:31

3K+ Views

Yes, we can turn a column records into a list using the MySQL GROUP_CONCAT(). Let us first create a table −mysql> create table DemoTable    -> (    -> ClientId int,    -> ClientName varchar(20)    -> ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table ... Read More

Create a temporary table with dates in MySQL

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:42:34

704 Views

To create a temporary table with dates, use the CREATE TEMPORARY TABLE in MySQL. Following is the syntax −Syntaxcreate temporary table yourTableName(    yourColumnName datetime );Let us first create a table −mysql> create temporary table DemoTable    -> (    -> DueDate datetime    -> ); Query OK, 0 rows ... Read More

MySQL query to increase item value price for multiple items in a single query?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:41:32

857 Views

To increase item value for multiple items in a single query, you can use the CASE statement in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> ProductName varchar(20),    -> ProductPrice int    -> ); Query OK, 0 rows affected (0.51 sec)Insert ... Read More

MySQL Select where value exists more than once

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:39:40

2K+ Views

For this, you can use GROUP BY HAVING along with the COUNT(*) function. Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert ... Read More

Implement MySQL query using multiple OR statements. Any optimal alternative?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:38:17

454 Views

It would be good to use IN() instead of multiple OR statements. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.83 sec)Insert some records ... Read More

Advertisements