AmitDiwan has Published 10740 Articles

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

AmitDiwan

AmitDiwan

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

196 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

382 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

198 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

142 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

8K+ 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

540 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

Update only a single column in a MySQL table and increment on the basis of a condition

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:56:39

148 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(50),    Score int ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Sam', 45); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable ... Read More

MySQL time period query to fetch date records from interval of 14 weeks from current date?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:54:26

291 Views

For this, you can use the BETWEEN keyword. Let us first create a table −mysql> create table DemoTable (    ArrivalDate date ); Query OK, 0 rows affected (0.93 sec)Let’s say the current date is 2019-08-31.Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-04-21'); Query ... Read More

How to insert NULL into char(1) in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:51:43

574 Views

For this, you need to set sql_mode to 'STRICT_TRANS_TABLES’. This mode issues a warning when an invalid value is inserted but inserts the same value. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(50),    Gender char(1) ... Read More

ORDER BY records in MySQL based on a condition

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:50:01

1K+ Views

For this, you can use ORDER BY IF(). Let us first create a table −mysql> create table DemoTable (    Name varchar(50),    Score int ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 98); Query OK, 1 ... Read More

Advertisements