
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Difference between two selects in MySQL?
You can use subqueries for difference between two selects in MySQL. The syntax is as follows:
SELECT *FROM yourTableName where yourColumnName NOT IN(SELECT yourColumnName FROM youTableName WHERE yourCondition;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table DifferenceSelectDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserId int, -> UserValue int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.87 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into DifferenceSelectDemo(UserId,UserValue) values(10,10); Query OK, 1 row affected (0.24 sec) mysql> insert into DifferenceSelectDemo(UserId,UserValue) values(10,20); Query OK, 1 row affected (0.15 sec) mysql> insert into DifferenceSelectDemo(UserId,UserValue) values(20,30); Query OK, 1 row affected (0.17 sec) mysql> insert into DifferenceSelectDemo(UserId,UserValue) values(20,20); Query OK, 1 row affected (0.19 sec) mysql> insert into DifferenceSelectDemo(UserId,UserValue) values(30,40); Query OK, 1 row affected (0.14 sec) mysql> insert into DifferenceSelectDemo(UserId,UserValue) values(30,20); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from DifferenceSelectDemo;
The following is the output:
+----+--------+-----------+ | Id | UserId | UserValue | +----+--------+-----------+ | 1 | 10 | 10 | | 2 | 10 | 20 | | 3 | 20 | 30 | | 4 | 20 | 20 | | 5 | 30 | 40 | | 6 | 30 | 20 | +----+--------+-----------+ 6 rows in set (0.02 sec)
Here is the query to get the difference between two selects:
mysql> select *from DifferenceSelectDemo -> WHERE UserValue NOT IN (select UserValue from DifferenceSelectDemo where Id=1);
The following is the output:
+----+--------+-----------+ | Id | UserId | UserValue | +----+--------+-----------+ | 2 | 10 | 20 | | 3 | 20 | 30 | | 4 | 20 | 20 | | 5 | 30 | 40 | | 6 | 30 | 20 | +----+--------+-----------+ 5 rows in set (0.09 sec)
- Related Articles
- MySQL difference between two timestamps in Seconds?
- Sorted difference between two columns in MySQL?
- Difference between two timestamps in seconds in MySQL?
- MySQL Merge selects together?
- Get the difference between two timestamps in seconds in MySQL?
- Find the difference between two timestamps in days with MySQL
- How to find absolute difference between two numbers in MySQL?
- Find the difference between two datetime values with MySQL?
- How can we calculate the difference between two time values in MySQL?
- Manipulate two selects on page load with jQuery
- How to optimize many SELECTs in a single table in MySQL?
- How to get the difference between two columns in a new column in MySQL?
- Difference between two strings JavaScript
- Difference between MySQL and MongoDB
- Difference Between MySQL and PostgreSQL

Advertisements