AmitDiwan has Published 10740 Articles

SET only two values for all the rows in a MySQL table based on conditions?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:40:10

175 Views

For condition based update, use UPDATE and IF(). 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 into DemoTable values(10); Query OK, 1 row affected (0.08 sec) ... Read More

How can I match a comma separated list against a value in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:38:18

1K+ Views

Let us first create a table −mysql> create table DemoTable (    `Values` varchar(50) ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('23, 45, 78, 56'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('384, ... Read More

MySQL query to fetch only a single field on the basis of boolean value in another field

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:35:20

273 Views

Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeName varchar(40),    isMarried boolean ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName, isMarried) values('Chris', true); Query OK, ... Read More

How to insert an array of values in a MySQL table with a single INSERT?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:32:56

5K+ Views

Let us first create a table −mysql> create table DemoTable (    ClientId int,    ClientName varchar(50) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command. Here, we are inserting multiple values using only a single INSERT −mysql> insert into DemoTable values(101, 'Adam'), ... Read More

How to structure some data in a MySQL database for easier retrieval?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:31:12

138 Views

You can use FIND_IN_SET to structure some data in a database for easier retrieval. Let us first create a table −mysql> create table DemoTable (    CountryName SET('US', 'UK', 'AUS') ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable ... Read More

Select rows from a table with date between 90 days ago and now in MySQL

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:29:11

2K+ Views

Use MySQL INTERVAL for this. Let us first create a table −mysql> create table DemoTable (    DueDate date ); Query OK, 0 rows affected (1.13 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-07-10'); Query OK, 1 row affected (0.10 sec) mysql> insert into ... Read More

A single MySQL query to insert records (not all) in the second table from the first table

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:27:12

164 Views

Use nested insert with select in MySQL for this as shown in the below syntax −insert into yourTableName2(yourColumnName1, yourColumnName2, .....N) select yourColumnName1, yourColumnName2, ....N from yourTableName1 where yourCondition;Let us first see an example and create a table −mysql> create table DemoTable1 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ... Read More

What happens when you insert nothing after declaring a column “timestamp default CURRENT_TIMESTAMP”?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:24:49

154 Views

In this case, the current timestamp gets inserted into the table column. Let us first create a table −mysql> create table DemoTable (    ArrivalDate timestamp default CURRENT_TIMESTAMP ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(now()); Query ... Read More

Convert a stored MD5 string to a decimal value in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:23:21

536 Views

You can use the conv() function along with the cast() to cast from hexadecimal to a decimal value.Note − MD5 is in hexadecimalLet us first create a table −mysql> create table DemoTable (    Password text ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using ... Read More

Which is the fastest way to get a column's maximum value in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:21:22

390 Views

You can try to get a column’s maximum value using two ways. The first way is as follows −select max(yourColumnName) from yourTableName;The second way is as follows −select yourColumnName from yourTableName order by yourColumnName DESC LIMIT 1;NOTE − The first query takes less time than the second query because the ... Read More

Advertisements