Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query problem

Author  Topic 

hamid.y
Starting Member

22 Posts

Posted - 2010-03-01 : 06:13:04
I wanna select all records which is equal with a set of IDs.

Table1:
ID | Name | city
-----------------
1 jack new york
2 john Ohio
3 jenny LA

Table2:
ID
-----
1
2

desirable result:
-----------------
every records in Table1 which its ID is Equal with Table2 ID

or this problem
----------------
select * from Table1
where ID = (select ID from Table2)

but pay attention (select ID from Table2) return more than one record.
and i want to select all Records of Table1 where its IDs is equal with all each ID returned by second select.

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-03-01 : 06:15:37
select * from Table1
where ID in (select ID from Table2)

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-03-01 : 06:22:11
try this too

select t.* from table1 t join table2 t2 on t.id = t2.id

select * from table1 t where exists (select * from table2 where id = t.id
Go to Top of Page

hamid.y
Starting Member

22 Posts

Posted - 2010-03-01 : 06:23:50
no no.
i want adopt something like for statement in interface programming.
i want select records of table1 where is equal with one by one records of table2.
thats it
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-01 : 10:46:17
quote:
Originally posted by hamid.y

no no.
i want adopt something like for statement in interface programming.
i want select records of table1 where is equal with one by one records of table2.
thats it


thats exactly what query suggested does.
did you try it?
if yes, why do you still think its not what you want?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-02 : 02:41:04
quote:
Originally posted by hamid.y

no no.
i want adopt something like for statement in interface programming.
i want select records of table1 where is equal with one by one records of table2.
thats it


Did you try running the queries?
Dont assume anything without running them

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -