MySQL Articles

Page 52 of 355

MySQL query to find a list of city names that do not start with vowels?

Arjun Thakur
Arjun Thakur
Updated on 30-Jun-2020 14K+ Views

You can use DISTINCT with RLIKE operator to find a list of city names that do not start with vowels.The syntax is as follows −SELECT DISTINCT yourCityColumnName FROM yourTableName WHERE yourCityColumnName NOT RLIKE ‘ ^[AEIOUaeiou].*$’;To understand the above syntax, let us create a table. Here, we have a column for city names.The query to create a table is as follows −mysql> create table Employee_Information    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> EmployeeName varchar(20),    -> CityName varchar(20),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the ...

Read More

MySQL Query to remove all characters after last comma in string?

Ankith Reddy
Ankith Reddy
Updated on 30-Jun-2020 4K+ Views

To remove all characters after the last comma in the string, you can use SUBSTRING_INDEX().If you do not know the location of the last comma, then you need to find the last comma dynamically using LENGTH().The syntax is as follows −UPDATE yourTableName set yourColumnName = SUBSTRING_INDEX(yourColumnName, ', ', LENGTH(yourColumnName) - LENGTH(REPLACE(yourColumnName, ', ', '')));To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table RemoveAllCharacters    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> FullInfo varchar(200),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 ...

Read More

Replace 0 with null in MySQL?

George John
George John
Updated on 30-Jun-2020 2K+ Views

You can use NULLIF() from MySQL to replace 0 with NULL. The syntax is as follows −SELECT *, NULLIF(yourColumnName, 0) as anyVariableName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table Replace0WithNULLDemo    -> (    -> Id int NOT NULL auto_increment,    -> Name varchar(20),    -> Marks int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.53 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into Replace0WithNULLDemo(Name, Marks) values('John', ...

Read More

Find out if a varchar contains a percent sign in MySQL?

Ankith Reddy
Ankith Reddy
Updated on 30-Jun-2020 514 Views

To find out a varchar contains a percent sign in MySQL, you can use LIKE operator. The syntax is as follows −SELECT * FROM yourTableName WHERE yourColumnName like '%|%%' escape '|';To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table FindPercentInVarcharDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Words varchar(30),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records with % sign using insert command. The query is as follows −mysql> insert into FindPercentInVarcharDemo(Words) values('This is ...

Read More

Change date format (in DB or output) to dd/mm/yyyy in PHP MySQL?

Chandu yadav
Chandu yadav
Updated on 30-Jun-2020 3K+ Views

You can change the date format in PHP using date() fucntion. The syntax is as follows −date(d/m/Y, yourDateTimeVariable);In PHP, convert string to date using strtodate(). Here is the PHP code used to format the datetime −$LogintDate = strtotime('2019-01-12'); echo date('d/m/Y', $LogintDate);The snapshot of code is as follows −The following is the output −12/01/2019You can achieve in MySQL with the help of date_format() function. The syntax is as follows −SELECT DATE_FORMAT(yourColumnName, ’%d/%m/%Y’) as anyVariableName FROM yourTableName;To understand the above syntax, let us create a table −mysql> create table Date_FormatDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> ...

Read More

MySQL LIMIT to select a single row

George John
George John
Updated on 30-Jun-2020 532 Views

To select a single row in MySQL, you can use LIMIT. At first, let us create a table. The query to create a table is as follows −mysql> create table selectWithPrimaryKey    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Age int,    -> Marks int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into selectWithPrimaryKey(Name, Age, Marks) values('Larry', 24, 98); Query OK, 1 row affected (0.15 sec) mysql> insert into ...

Read More

Convert dd/mm/yyyy string to Unix timestamp in MySQL?

Arjun Thakur
Arjun Thakur
Updated on 30-Jun-2020 579 Views

Convert dd/mm/yyyy string to Unix timestamp with the help of UNIX_TIMESTAMP(). The syntax is as follows −SELECT UNIX_TIMESTAMP(STR_TO_DATE(yourColumnName, '%d/%m/%Y')) as anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table ConvertddmmyyyyInUnixTimeStamp    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Created_at varchar(30),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into ConvertddmmyyyyInUnixTimeStamp(Created_at) values('10/11/2012'); Query OK, 1 row affected (0.21 sec) mysql> ...

Read More

MySQL get hash value for each row?

Ankith Reddy
Ankith Reddy
Updated on 30-Jun-2020 2K+ Views

Get hash value of each row using MD5() function from MySQL. The syntax is as follows −SELECT MD5(CONCAT(yourColumnName1, yourColumnName2, yourColumnName3, .......N)) as anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table getHashValueForEachRow    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Age int,    -> Marks int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (1.25 sec)Insert records in the table using insert command. The query is as follows −mysql> insert into getHashValueForEachRow(Name, Age, ...

Read More

How to map keys to values for an individual field in a MySQL select query?

Chandu yadav
Chandu yadav
Updated on 30-Jun-2020 3K+ Views

You can use CASE statement in MySQL to map keys to values for an individual field in select query. The syntax is as follows −SELECT yourColumnName1, yourColumnName2, yourColumnName3, .........N (    CASE WHEN yourColumnName = 1 THEN 'ENABLED'    ELSE 'DISABLED'    END ) AS anyVariableName FROM yourTableName;You can use IF() function also for the same purpose. The syntax is as follows −SELECT yourColumnName1, yourColumnName2, yourColumnName3, .........N ,IF(yourColumnName, 'ENABLED', 'DISABLED') as anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table MapKeys    -> (   ...

Read More

MySQL order by string with numbers?

George John
George John
Updated on 30-Jun-2020 6K+ Views

To MySQL order string with numbers, the following is the syntax, wherein we have used ORDER BY, SUBSTR() and CAST() −SELECT *FROM yourTableName ORDER BY SUBSTR(yourColumnName FROM 1 FOR 2), CAST(SUBSTR(yourColumnName FROM 2) AS UNSIGNED);To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table OrderByStringWithNumbers    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Words varchar(10),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert ...

Read More
Showing 511–520 of 3,547 articles
« Prev 1 50 51 52 53 54 355 Next »
Advertisements