Christina
Christina

Reputation:

inner join for a query?

I want to do a sql query and have some problems:

  1. I want to salect from table_1 the ID's Where parent_id is the value I have:

    SELECT ID FROM table_1 WHERE parent_ID = 'x'

  2. I want to use the ID'S I got in 1. and

    SELECT FROM table_2 WHERE ID = 'The ID's from Query 1.

Upvotes: 0

Views: 134

Answers (2)

Query Master
Query Master

Reputation: 7097

Simple and Execute

Select t1.`id` FROM table t1 INNER JOIN table t2 ON t1.`id`=t2.`id`

Upvotes: 3

Manzabar
Manzabar

Reputation: 676

As Bainternet mentioned you can do this with a subquery

SELECT * FROM table_2 WHERE ID IN (SELECT ID FROM table_1 WHERE parent_ID = 'x')

Though your idea to use an inner join is also good (especially since MySQL can be slow when processing subqueries).

SELECT t2.* FROM table_2 as t2 INNER JOIN table_1 AS t1 ON t2.ID = t1.ID WHERE t1.parent_ID = 'x'

If that's not clear, try looking at the MySQL JOIN Syntax or the Subqueries, as Bainternet mentioned. If these examples and the MySQL docs aren't clear enough for you, consider posting more details on exactly what you're trying to do (e.g. include table structures in your question). Also while you might want this information for some WordPress related work you are doing, there's nothing in the question itself that actually ties it to WordPress. So if you have more questions that about MySQL queries in general, then you might want to consider posting them to the StackOverflow, tagged as mysql-query.

Upvotes: 2

Related Questions