- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL Select Rows where two columns do not have the same value?
You can use != operator from MySQL for this. The syntax is as follows:
SELECT *FROM yourTableName WHERE yourColumnName1 !=yourColumnName2 OR (yourColumnName1 IS NULL AND yourColumnName2IS NOT NULL) OR (yourColumnName2 IS NULL AND yourColumnName1 IS NOT NULL);
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table selectTwoColumns -> ( -> Id int NOT NULL AUTO_INCREMENT, -> FirstNumber int, -> SecondNumber 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 selectTwoColumns(FirstNumber,SecondNumber) values(10,20); Query OK, 1 row affected (0.29 sec) mysql> insert into selectTwoColumns(FirstNumber,SecondNumber) values(30,40); Query OK, 1 row affected (0.16 sec) mysql> insert into selectTwoColumns(FirstNumber,SecondNumber) values(20,20); Query OK, 1 row affected (0.15 sec) mysql> insert into selectTwoColumns(FirstNumber,SecondNumber) values(50,60); Query OK, 1 row affected (0.18 sec) mysql> insert into selectTwoColumns(FirstNumber,SecondNumber) values(50,50); Query OK, 1 row affected (0.17 sec) mysql> insert into selectTwoColumns(FirstNumber,SecondNumber) values(70,NULL); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from selectTwoColumns;
The following is the output:
+----+-------------+--------------+ | Id | FirstNumber | SecondNumber | +----+-------------+--------------+ | 1 | 10 | 20 | | 2 | 30 | 40 | | 3 | 20 | 20 | | 4 | 50 | 60 | | 5 | 50 | 50 | | 6 | 70 | NULL | +----+-------------+--------------+ 6 rows in set (0.00 sec)
Here is the query to select rows where two columns do not have same value:
mysql> select *from selectTwoColumns -> where FirstNumber!=SecondNumber -> OR (FirstNumber IS NULL AND SecondNumber IS NOT NULL) -> OR (SecondNumber IS NULL AND FirstNumber IS NOT NULL);
The following is the output:
+----+-------------+--------------+ | Id | FirstNumber | SecondNumber | +----+-------------+--------------+ | 1 | 10 | 20 | | 2 | 30 | 40 | | 4 | 50 | 60 | | 6 | 70 | NULL | +----+-------------+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- SELECT MySQL rows where today's date is between two DATE columns?
- Count how many rows have the same value in MySQL?
- How do I SELECT none of the rows and columns in MySQL?
- SELECT not null column from two columns in MySQL?
- Select all duplicate MySQL rows based on one or two columns?
- Select MySQL rows where column contains same data in more than one record?
- Find rows that have the same value on a column in MySQL?
- Select from table where value does not exist with MySQL?
- Return records that do not have a value in a certain field with two SELECT statement in a single MySQL query
- MySQL query to select column where value = one or value = two, value = three, etc?
- Get rows that have common value from the same table with different id in MySQL
- MySQL query to select rows where column value is only 0, group by another column?
- How do you select from MySQL where last value in a string = x?
- MYSQL select DISTINCT values from two columns?
- How to select a row where one of several columns equals a certain value in MySQL?

Advertisements