Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to select table from select other table ?

Table1
==========
ID| Name |
----------
1 | Henry
2 | Dony
----------

Table2
===========
ID| Addres|
-----------
1 | London
2 | Texas
-----------
i have select ID table2 From Select table1, like below :
SQL
SELECT ID From Table2 Where Select ID From Table1
Posted
Comments
[no name] 26-Jan-15 12:15pm    
SELECT TABLE2.ID (and others you need) FROM TABLE2 LEFT JOIN TABLE1 ON TABLE1.ID = TABLE2.ID WHERE NOT TABLE1.ID IS NULL.
Member 11057420 26-Jan-15 13:08pm    
thx you, this solved my problem

Your table examples and description don't give much to go on as the relationship of the tables is not known. I can only guess on the ID columns but I am unsure.

If you are looking at joining the information in the 2 tables on matching information I would start with inner join[^].

you can also look up information on outer joins, I would Google on "TSQL left outer join" if you want to show information that is in the left table but not the right table

P.S I have made an assumption that you are looking at SQL Server, If I am wrong please let know and I'll update my answer.
 
Share this answer
 
You can use the IN clause.
select id from table2 where id IN (select id from table1 where <<some condition>>)

The same query can be written as a JOIN. You can try that out yourself.
 
Share this answer
 
Comments
[no name] 26-Jan-15 14:03pm    
Or also EXISTS but IN and EXISTS are usually not that performant, especially on tables with a lot of records. This is just by experience, don't ask me why it is like that ;)

The above is true at least for EXISTS, IN I have to check again, I think a light goes on in my brain....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900