I have a mysql table like this:

I’m trying to get a list of nid values where tid is 19 AND any value from 53, 16 or 89. As per the current table, I’d expect the query returns 10 which is =19 (4th row) and IN (53,16,89) (last row).
I could get this as 2 separate queries like these:
SELECT nid FROM {term_node} WHERE tid IN (53,16,89)
SELECT nid FROM {term_node} WHERE tid = 19
(term_node is the table in screenshot). I’m using PHP for this application so I can combine results of these 2 queries to get the desired result (which is 10). But I think there could be a way to get this from a single database query.
I read about Union but honestly I don’t know how to make use of it.
I hope I’m clear with the question and thank you very much in advance.
A
UNIONwon’t help you here. What you want is an intersection. Some databases have anINTERSECTkeyword that does what you want, but not MySQL unfortunately.You can get what you want by using an inner join:
See it working online: sqlfiddle