0

I want to get specific column values from one table and use those values to get information from another table.

I'll use this example: both table1 and table2 contains rows with a "name" column.

I want to select the values of all the "name" columns from table1 meeting conditions and then select rows from table2 which contain any of the names selected from table1. The basic idea is below.

SELECT `name` FROM table1 WHERE...
SELECT `name` FROM table2 WHERE `name` IN(names from the above query)

Hopefully this is clear, thanks.

2
  • @KayKay I'm not how you go about SELECTing from one table and using those results to SELECT from another table, all in one query. Commented Nov 30, 2013 at 22:50
  • While you can indeed do it with a SELECT within the IN clause, it would be much better to do it with a JOIN instead. Commented Nov 30, 2013 at 22:57

2 Answers 2

1

Just "inject" your first query in the second :

SELECT name FROM table2 WHERE name IN(SELECT name FROM table1 WHERE...)
Sign up to request clarification or add additional context in comments.

Comments

0

Apart from the answer from KayKay you can also use EXISTS:

SELECT name FROM table2 t2
WHERE EXISTS( SELECT 1
              FROM table t1
              WHERE t2.name = t1.name)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.