Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL NOT IN from another column in the same table

Tags:

sql

mysql

I want to run a mysql query to select all rows from a table films where the value of the title column does not exist anywhere in all the values of another column (collection).

Here is a simplified version of my table with content:

mysql> select * from films;
+----+--------------+--------------+
| id | title        | collection   |
+----+--------------+--------------+
|  1 | Collection 1 | NULL         |
|  2 | Film 1       | NULL         |
|  3 | Film 2       | Collection 1 |
+----+--------------+--------------+

Here is my query:

mysql> SELECT * FROM films WHERE title NOT IN (SELECT collection FROM films);
Empty set (0.00 sec)

In this example, I would want to select the rows with titles Film 1 and Film 2, but my query is returning no rows.

Here is the table structure:

CREATE TABLE `films` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) NOT NULL DEFAULT '',
  `collection` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;
like image 900
jessica Avatar asked Jan 09 '13 20:01

jessica


People also ask

What is the use of <> in MySQL?

The symbol <> in MySQL is same as not equal to operator (!=). Both gives the result in boolean or tinyint(1). If the condition becomes true, then the result will be 1 otherwise 0.

How do you get records which are not in another table in MySQL?

We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

Is not between in MySQL?

MySQL NOT BETWEEN AND operator checks whether a value is not present between a starting and a closing expression. If expr is not greater than or equal to min and expr is not less than or equal to max, BETWEEN returns 1, otherwise, it returns 0.

How do I match two columns in MySQL?

Here's the generic SQL query to two compare columns (column1, column2) in a table (table1). mysql> select * from table1 where column1 not in (select column2 from table1); In the above query, update table1, column1 and column2 as per your requirement.


2 Answers

SELECT * 
FROM films 
WHERE title NOT IN (SELECT collection FROM films where collection is not null);

SQLFiddle: http://sqlfiddle.com/#!2/76278/1

like image 95
a_horse_with_no_name Avatar answered Sep 30 '22 16:09

a_horse_with_no_name


Have you tried using NOT EXISTS:

SELECT * 
FROM films f1
WHERE NOT EXISTS (SELECT collection 
                  FROM films f2
                  WHERE f1.title = f2.collection);

See SQL Fiddle with Demo

If you want to use IN then you will want to look for values that are NOT NULL:

SELECT * 
FROM films 
WHERE title NOT IN (SELECT collection 
                    FROM films
                    WHERE collection is not null);

See SQL Fiddle with Demo

The result for both is:

| ID |  TITLE |   COLLECTION |
------------------------------
|  2 | Film 1 |       (null) |
|  3 | Film 2 | Collection 1 |

The problem with your current query is that -- stealing from @Quassnoi's answer here:

Both IN and NOT IN return NULL which is not an acceptable condition for WHERE clause.

Since the null value is being returned by your subquery you want to specifically exclude it.

like image 38
Taryn Avatar answered Sep 30 '22 16:09

Taryn