AmitDiwan has Published 11365 Articles

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

70 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

104 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

379 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

284 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

Selecting from a MySQL table based on parts of a timestamp?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:19:07

116 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate timestamp ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2007-01-10'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('2015-07-12'); Query OK, 1 ... Read More

MySQL query to fetch column length declared with BLOB type

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:16:58

269 Views

For this, use the LENGTH() function from MySQL. Let us first create a table. We have declared the type of column as BLOB −mysql> create table DemoTable (    Title blob ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

How to swap a specific field value in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:14:13

87 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Number1 int,    Number2 int ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number1, Number2) values(10, 30); Query OK, ... Read More

Searching 2 fields at the same time to fetch a specific First Name and Last Name from a table in MySQL

AmitDiwan

AmitDiwan

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

65 Views

For this, you can use LIKE operator along with AND. Let us first create a table −mysql> create table DemoTable (    EmployeeFirstName varchar(50),    EmployeeLastName varchar(50) ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 'Smith'); Query ... Read More

Can we use “LIKE concat()” in a MySQL query?

AmitDiwan

AmitDiwan

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

6K+ Views

Yes, we can do that. Let us first create a table −mysql> create table DemoTable (    Name varchar(50) ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into ... Read More

MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:00:00

443 Views

For this, use RAND() for random records and LIMIT 1 to get only a single value. However, use the WHERE clause to select that particular ‘Name’, which is repeating.Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20) ... Read More

Advertisements