Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Query Exclude a certain entry

Tags:

mysql

set

I'm trying to SELECT entries of a certain type, name, EXCLUDING entries with the same name in a particular id. The last part is the tricky part that I can't get my head around.

SELECT name FROM table WHERE type = $type AND (name is not contained in an entry with id). 

I want the result to be the set of all names that ARE of a certain type but NOT already included in a current id.

Do I need to execute two queries here? Or can I condense it to one.

Thanks.

like image 810
Dirk Avatar asked Aug 08 '26 20:08

Dirk


1 Answers

You can use a subquery for this:

SELECT name
FROM table
WHERE type = $type
AND name NOT IN (SELECT entry FROM t WHERE id = $id );
like image 171
Zed Avatar answered Aug 10 '26 08:08

Zed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!