Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not exists in the same table

Tags:

sql

I want to select the tests that run on IE7 and not run on IE8. I tried this but i get 0 and this is not true.

SELECT test_name
FROM tests 
WHERE version='ie7' 
AND NOT EXISTS (SELECT test_name FROM tests where version='ie8');

Thank you!

like image 866
Ronny Avatar asked Dec 23 '10 13:12

Ronny


People also ask

How do you check if data not exists in a table SQL?

To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.

How do you use exists and not exists in one query?

If EXISTS (subquery) returns at least 1 row, the result is TRUE. If EXISTS (subquery) returns no rows, the result is FALSE. If NOT EXISTS (subquery) returns at least 1 row, the result is FALSE. If NOT EXISTS (subquery) returns no rows, the result is TRUE.

When to use in or exists?

“IN” clause is preferred when there is a small list of static values or the inner query returns a very less number of rows. “EXISTS” clause is preferred when there is a need to check the existence of values in another table or when there is a need to check against more than one column.


2 Answers

Try this

SELECT test_name
FROM tests 
WHERE version='ie7' 
AND test_name  NOT In (SELECT test_name FROM tests where version='ie8');
like image 71
Suvabrata Roy Avatar answered Sep 18 '22 20:09

Suvabrata Roy


You probably mean:

SELECT test_name
FROM tests t1
WHERE version='ie7' 
AND NOT EXISTS (SELECT test_name FROM tests t2 where test_name = t1.test_name AND version='ie8');

(My Transact-SQL is a bit rusty, but I think this is how it is done. The important thing is that you were saying "I want all test_names with version 'ie7' and also that no row exists in the database at all with version 'ie8'" :) )

like image 25
Spiny Norman Avatar answered Sep 19 '22 20:09

Spiny Norman