'Mysql merging two queries into one

Trying to merge below two queries into one in the most "correct/efficient" way:

select id from TABLE1 where apple='green'
while{
select * from TABLE2 where id=[id from TABLE1 above]
while{

Eg query one will return a number of id's which in turn will return a number of rows in query two.



Solution 1:[1]

You can try to use EXISTS subquery

SELECT * 
FROM TABLE2 t2
WHERE EXISTS(
    SELECT 1 
    FROM TABLE1 t1
    WHERE t1.apple='green' AND t1.id = t2.id
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D-Shih