MySQL - Select rows with id from another table

Discuss coding issues, and scripts related to PHP and MySQL.
PloMar
Posts:48

MySQL - Select rows with id from another table

I have the following Select in MySQL:

Code: Select all

$sql = "SELECT * FROM table_1 WHERE rank = 2 AND id IN(2, 5, 8)";
But the IDs (2, 5, 8, ...) must be from a Select from another table.
So, how can I select the rows from table_1 with the IDs from table_2?

MarPlo Posts:186
You can use multiple Select statements in the same SQL query.
Try this query, using a sub-select that selects the IDs.

Code: Select all

$sql = "SELECT * FROM table_1 WHERE rank = 2 AND id IN(SELECT id FROM table_2 WHERE condition)";
- Replace "condition" with the condition that you want to select the IDs from table_2.