Say I have a table called "people". I want to select all of the people that have a certain value in column "town". Then I want to select all the rows in a different table which have a value that exists in the previous selection.
Asked
Active
Viewed 85 times
-2
-
2Sample data and desired results would really help. – Gordon Linoff Dec 05 '18 at 03:36
-
Possible duplicate of [MySQL: Quick breakdown of the types of joins](https://stackoverflow.com/questions/6294778/mysql-quick-breakdown-of-the-types-of-joins) – But those new buttons though.. Dec 05 '18 at 03:38
1 Answers
0
Two options, both involve a sub-query...
Use an
EXISTS
clauseSELECT * FROM `a different table` dt WHERE EXISTS ( SELECT 1 FROM people p WHERE p.town = 'a certain value' AND p.value = dt.value -- you didn't specify column names )
Use an
IN
clauseSELECT * from `a different table` dt WHERE dt.value IN ( SELECT p.value FROM people p WHERE p.town = 'a certain value' )
Personally, I'd go with the first option but depending on your table types and indexes, you should run some explain-plans to see which is more performant.

Phil
- 157,677
- 23
- 242
- 245