sql – Alternative to except in MySQL

sql – Alternative to except in MySQL

You could use NOT IN

SELECT * 
FROM Tab1
WHERE id  NOT IN (
    SELECT id 
    FROM Tab1 
    WHERE int_attribute_of_Tab1>0
)

Try this

SELECT * 
FROM Tab1
WHERE [....] NOT EXISTS 
(SELECT * 
FROM Tab1 
WHERE int_attribute_of_Tab1>0) 

sql – Alternative to except in MySQL

A couple of definitions
SqlServer https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql EXCEPT
Returns any distinct values from the query to the left of the EXCEPT operator that are not also returned from the right query.
PLsql https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm MINUS
statement combines results with the MINUS operator, which returns only unique rows returned by the first query but not by the second

A pedantic translation to mysql would be

 SELECT distinct t1.* 
FROM Tab1 as t1
left outer join
(SELECT * 
FROM Tab1 
WHERE int_attribute_of_Tab1>0) as t2 on t1.id = t2.id
where t2.id is null;

Assuming there is an id column, And I wouldnt like to use distinct on a lot of columns.

Leave a Reply

Your email address will not be published.