elSama elSama - 7 months ago 11
SQL Question

Perform a select query based on a previous query

I would like to perform two queries on a number of tables. The first query joins several tables and uses several conditions. It is something like this:

SELECT items.id, tableX.colA, tableY.colB ...
FROM items
LEFT JOIN table2 ON ...
LEFT JOIN table3 ON ...
...
WHERE {some conditions are true}


The second query is a simple query, but depends on the result of the first one. I would like to select all rows related to the previous query by their id. So it should be something like this

SELECT meta_name, meta_value FROM items_meta WHERE item IN ({LIST OF IDS FROM PREVIOUS QUERY})


The two queries will be used for different purposes so they cannot be merged into one.

I've been thinking of storing the result of the first query in a temporary table, then JOIN the second query to it. But I can't seem to find a decent way of returning the rows of the first query and putting the IDs into a temporary table.

All contributions are greatly appreciated.
Thanks

Answer

You have to design your ID's like i use in the query. You can use the join function of PHP here.

Query

SELECT meta_name, meta_value FROM items_meta WHERE item IN ('4', '8', '11')