AmitDiwan has Published 10744 Articles

Select all records if it contains specific number in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 13:15:53

238 Views

For this, use concat() along with LIKE. Following is the syntax −select *from yourTableName where concat(', ', yourColumnName, ', ') like '%, yourValue, %';Let us create a table −mysql> create table demo49 −> ( −> id varchar(20) −> , −> first_name varchar(20) −> ); Query OK, 0 rows affected (1.45 ... Read More

Append wildcards in SELECT with MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 13:09:03

140 Views

For appending, use the concept of concat(). The syntax is as follows −select *from yourTableName where yourColumnName like concat('%', yourValue, '%');Let us create a table −mysql> create table demo48 -> ( −> id int not null auto_increment primary key, −> name varchar(20) −> ); Query OK, 0 rows affected (0.70 ... Read More

What is the MySQL syntax error in this query – Creating a table with reserved keyword?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 13:07:15

574 Views

Let’s say we tried creating a table with name “groups”, which is a reserved keyword in MySQL You cannot use “groups” because groups is a reserved keyword in MySQL.Following error occurred while creating a table with name “groups” −mysql> create table groups −> ( −> id int, −> name varchar(40) ... Read More

Remove specific fields/ rows and show other records in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 13:05:15

153 Views

For this, use CASE WHEN statement in MySQL. Let us create a table −mysql> create table demo47 −> ( −> first_name varchar(20), −> last_name varchar(20) −> ); Query OK, 0 rows affected (1.57 sec)Insert some records into the table with the help of insert command −mysql> insert into demo47 values('John', ... Read More

How to convert MM/YY to YYYY-MM-DD with a specific day in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 13:03:05

283 Views

To convert, use STR_TO_DATE(), as in the below syntax. Concatenate the day value with CONCAT() −select str_to_date(concat('yourDateValue/', yourColumnName), '%d/%m/%y') as anyAliasName from yourTableName;Let us create a table −mysql> create table demo46 −> ( −> id int not null auto_increment primary key, −> short_date varchar(20) −> ); Query OK, 0 rows ... Read More

MySQL REGEXP to fetch string + number records beginning with specific numbers?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 13:01:41

278 Views

For this, use REGEXP and fetch records beginning with specific numbers. Following is the syntax:Select yourColumnName1, yourColumnName2 from yourTableName where yourColumnName2 REGEXP '^yourStringValue[yourNumericValue]';Let us create a table −mysql> create table demo45 -> ( −> id int not null auto_increment primary key, −> value varchar(50) −> ); Query OK, 0 rows ... Read More

Select highest salary in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 13:00:16

1K+ Views

For this, you can use MAX(). The syntax is as follows −select MAX(yourColumnName) AS anyAliasName from yourTableName;Let us create a table −mysql> create table demo44 −> ( −> employee_id int not null auto_increment primary key, −> employee_name varchar(20), −> employee_salary int −> ) −> ; Query OK, 0 rows affected ... Read More

Creating a Table in MySQL to set current date as default

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:58:22

10K+ Views

Following is the syntax for creating a table and adding DEFAULT constraint to set default value −CREATE TABLE yourTableName ( yourColumnName1 dataType not null , yourColumnName2 dataType default anyValue, . . . N );;Let us create a table wherein we have set “employee_joining_date” with default constraint for current date as ... Read More

SELECT a row by subtracting dates in WHERE in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:55:29

195 Views

For this, use TIMESTAMPDIFF(). Let us create a table −mysql> create table demo42 −> ( −> start_date datetime −> ); Query OK, 0 rows affected (0.77 sec)Insert some records into the table with the help of insert command −mysql> insert into demo42 values('2020-01-10 12:30:05'); Query OK, 1 row affected (0.11 ... Read More

MySQL regexp to display only records with strings or strings mixed with numbers. Ignore only the number records

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:54:06

167 Views

For this, you can use REGEXP. Following is the syntax −select yourColumnName from yourTableName where yourColumnName REGEXP '[a−zA&minu;Z]';Let us create a table −mysql> create table demo41 −> ( −> name varchar(40) −> ); Query OK, 0 rows affected (0.64 sec)Insert some records into the table with the help of insert ... Read More

Advertisements