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
 General SQL Server Forums
 New to SQL Server Programming
 sql query

Author  Topic 

kabucek
Starting Member

7 Posts

Posted - 2008-10-14 : 10:31:10
hello @LL

is it possible to make a query like this?

- providing names of table1 & table2.

- display data from table1 where userID in table1 is the same as userID in table2 ?


is this possible ?

Thanks

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-14 : 10:33:15
yes its called inner join.
select a.* from table1 a inner table1 b where a.userID = b.userID
Go to Top of Page

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2008-10-14 : 10:47:51
quote:
Originally posted by hanbingl

yes its called inner join.
select a.* from table1 a inner join table1 b where a.userID = b.userID


Go to Top of Page

kabucek
Starting Member

7 Posts

Posted - 2008-10-14 : 10:49:44
thanks,

what about if i want to do query2 based on the results of query1 ?

Go to Top of Page

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2008-10-14 : 10:51:37
quote:
Originally posted by kabucek

thanks,

what about if i want to do query2 based on the results of query1 ?





select b.* from table1 a inner join table1 b where a.userID = b.userID
Go to Top of Page

kabucek
Starting Member

7 Posts

Posted - 2008-10-14 : 10:58:32
thanks,

i got this with this command:

SELECT `first`, `last`, `street1`, `street2`, `city`, `state`, `zip`, `emailAdr` FROM `table1`, `table2` WHERE table1.userID = table2.userID


- but what if i want to do another query based on the results of previous query ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-14 : 11:05:43
another query? what should you return in that based on first?
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-14 : 11:10:37
quote:
Originally posted by karthik_padbanaban

quote:
Originally posted by hanbingl

yes its called inner join.
select a.* from table1 a inner join table1 b where a.userID = b.userID






lol thanks, i didn't get my coffee this morning
Go to Top of Page

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2008-10-14 : 11:13:14
quote:
Originally posted by kabucek

thanks,

i got this with this command:

SELECT `first`, `last`, `street1`, `street2`, `city`, `state`, `zip`, `emailAdr` FROM `table1`, `table2` WHERE table1.userID = table2.userID


- but what if i want to do another query based on the results of previous query ?



you want some thing like this??????

select * from table1 where userid in (select userid from table2)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-14 : 11:29:18
better to use exists than in

select * from table1 t1 where exists (select * from table2 where userid=t1.userid)
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-14 : 11:51:38
quote:
Originally posted by kabucek

thanks,

i got this with this command:

SELECT `first`, `last`, `street1`, `street2`, `city`, `state`, `zip`, `emailAdr` FROM `table1`, `table2` WHERE table1.userID = table2.userID


- but what if i want to do another query based on the results of previous query ?




You mean something like:

SELECT `first`, `last`, `street1`, `street2`, `city`, `state`, `zip`, `emailAdr` FROM `table1`, `table2` WHERE table1.userID = table2.userID
and first = 'John' and last = 'Smith'

???
Go to Top of Page
   

- Advertisement -