Edward144 Edward144 - 4 months ago 25
MySQL Question

MySQL Check if row contains value from another table

I need to check if a row in a MySQL table contains a value stored in another table.

This is what I am trying right now:

SELECT * FROM table1 WHERE NOT EXISTS (SELECT * FROM table2 WHERE Column = '%');

This still finds rows which do contain the value in the second table. I have also tried exchanging EXISTS for LIKE and IN which both return syntax errors.


Using the % Sign seems to cause an issue. Here are some screenshots of what happens when I use the percent sign:
enter image description here

One of the values stored in the second table is 'Meath', the row containing 'Meath' is still selected. If I add 'Meath' instead of '%' then the row isn't returned:
enter image description here

Sam Sam
SELECT * FROM table1 a
WHERE a.County NOT IN(SELECT County FROM table2)

This should match what you want.

Edit: if you need to only disregard certain values from table2, you could replace the second line with this

WHERE a.County NOT IN(SELECT County FROM table2 WHERE County IN('county1', 'county2'))