Count Values Greater and Less Than a Specific Number in MySQL

AmitDiwan
Updated on 09-Oct-2019 12:07:56

1K+ Views

For this, you can use COUNT() along with CASE STATEMENT. Let us first create a table −mysql> create table DemoTable (    Score int ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.77 sec) mysql> insert into DemoTable values(48); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(59); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(33); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(38); Query OK, 1 row affected (0.09 sec) ... Read More

Select Specific Record from MySQL with Varchar Date Format

AmitDiwan
Updated on 09-Oct-2019 12:06:06

264 Views

For this, use STR_TO_DATE(). Let us first create a table −mysql> create table DemoTable (    DueDate varchar(60) ) ; Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('12-AUG-2016'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('14-AUG-2018'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('24-AUG-2012'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('14-AUG-2012'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

Find Duplicates in MySQL and Display in a Single Line

AmitDiwan
Updated on 09-Oct-2019 12:04:05

296 Views

For this, you can use GROUP_CONCAT() along with GROUP BY clause. Both are used to group concat duplicates and display in a single line. Let us first create a table −mysql> create table DemoTable (    StudentFavouriteSubject varchar(40),    StudentName varchar(40) ) ; Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MySQL', 'Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('MongoDB', 'Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('MySQL', 'Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into ... Read More

Search for Particular Numbers in MySQL Column with Comma-Separated Records

AmitDiwan
Updated on 09-Oct-2019 11:59:14

306 Views

Yes, you can search for particular numbers using the MySQL FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable (    ListOfNumbers varchar(100) ); Query OK, 0 rows affected (1.24 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('784, 746, 894, 344'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('456, 322, 333, 456'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('654, 785, 678, 456'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values('123, 676, 847, 785'); Query OK, 1 row affected (0.34 ... Read More

MySQL Query to Group Concat Data into a Single Row

AmitDiwan
Updated on 09-Oct-2019 11:56:00

141 Views

For this, use GROUP_CONCAT(). For only 1 values, work with MySQL WHERE clause. Let us first create a table −mysql> create table DemoTable (    PlayerName varchar(40),    PlayerStatus tinyint(1) ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 1); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David', 0); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Sam', 1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Carol', 1); Query OK, 1 row affected (0.12 sec) mysql> insert ... Read More

Remove XXX from Zip Code Field Using MySQL REGEXP

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

241 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, 1 row affected (0.23 sec) mysql> insert into DemoTable values('12345-674'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('89893-890'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('AAAAA-783'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL Select from Two Tables with a Single Query

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

3K+ 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') ; Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1(FirstName) values('Adam'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1(FirstName) values('Sam'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+----+-----------+ ... Read More

Get Row Data for Lowest and Highest Values in a MySQL Column

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

554 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 using insert command −mysql> insert into DemoTable values('Chris', 3599); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David', 7843); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Mike', 97474); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Bob', 65884); Query OK, ... Read More

Avoid Too Many OR Statements in a MySQL Query

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

261 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 DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name) values('Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Name) values('David'); ... Read More

Select Different Values from Same Column in MySQL

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 using insert command −mysql> insert into DemoTable(Name, Score) values('Chris', 45); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(Name, Score) values('David', 68); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, Score) values('Robert', 89); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name, Score) ... Read More

Advertisements