AmitDiwan has Published 11365 Articles

How to remove -XXX from Zip Code field using MySQL REGEXP?

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:52:27

120 Views

The easiest way to achieve this is by using the MySQL SUBSTRING_INDEX() function. Let us first create a table −mysql> create table DemoTable (    ZipCode varchar(50) ); Query OK, 0 rows affected (2.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('52533-909'); Query OK, ... Read More

MySQL SELECT from two tables with a single query

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:49:47

2K+ Views

Use UNION to select from two tables. Let us first create a table −mysql> create table DemoTable1 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20) ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(FirstName) values('Chris') ... Read More

Get row data for the lowest and highest values in a MySQL column

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:45:53

407 Views

For the lowest values in a MySQL column, use the MIN() method and for highest, use the MAX() method. Let us first create a table −mysql> create table DemoTable (    CustomerName varchar(20),    ProductAmount int ) ; Query OK, 0 rows affected (1.03 sec)Insert some records in the table ... Read More

How can I avoid too many OR statements in a MySQL query?

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:40:36

189 Views

Use MySQL IN() to avoid too many OR statements. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40) ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

How to select different values from same column and display them in different columns with MySQL?

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:38:11

2K+ Views

To select different values on the basis of condition, use CASE statement. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40), Score int ) ; Query OK, 0 rows affected (0.54 sec)Insert some records in the table ... Read More

Randomly SELECT distinct rows in a MySQL table?

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:32:15

590 Views

To randomly select rows, use ORDER BY RAND() with LIMIT. Use DISTINCT for distinct rows. Let us first see an example and create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40) ); Query OK, 0 rows affected (0.54 sec)Insert some ... Read More

Use delimiter correctly in a MySQL stored procedure to avoid BEGIN/END statements errors

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:20:01

101 Views

Such errors arise when you avoid using the DELIMITER concept. Let us see an example and run a query for stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE correct_procedure()    BEGIN    SELECT 'Hello MySQL !!!';    END // Query OK, 0 rows affected (0.12 sec) mysql> DELIMITER ;Following is ... Read More

Set new delay time in a MySQL column

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:18:35

170 Views

To set new delay time, use INTERVAL and update the column wth SETa clause and UPDATE command. Let us first create a table −mysql> create table DemoTable (    DelayTime time ); Query OK, 0 rows affected (1.21 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

MySQL RegEx to find lines containing N semi-colons?

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:15:40

175 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY ,    Title text ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Title) values('This is; a; MySQL;Tutorial'); Query OK, 1 ... Read More

How to find tables with a specific column name in MySQL?

AmitDiwan

AmitDiwan

Updated on 09-Oct-2019 11:12:56

968 Views

To find column names, use information_schema.columns. Following is the syntax −select distinct table_name from information_schema.columns where column_name like '%yourSearchValue%' and table_schema=database();Let us implement the above syntax in order to find column names across various table. Here, we want only table names with a specific column name word “Client” −mysql> select ... Read More

Advertisements