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
 where ... in

Author  Topic 

Davide70
Starting Member

6 Posts

Posted - 2012-02-16 : 11:44:28
Hello,
I have a select query which gives a column of values:
...
390
396
395
1207

Then I have a second query like this:

select *
from table
where id in (first query)

I would expect all the rows corresponding to the IDs in the column above, instead I only get one (in this case, the last one).
Any idea??
Thank very much

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-16 : 11:47:04
It would help if you post the actual query...only some of us are mind readers

SELECT * FROM TABLE2 WHERE ID IN (SELECT ID FROM TABLE1)

???

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Davide70
Starting Member

6 Posts

Posted - 2012-02-16 : 12:00:24
Hello Brett,
yes, it is more or less the way you put it. The first select gives a column with all the IDs I need. Then I perform a second query:
select * from Table2
where table2.id in (select id from table1 where ...)

Thank you,


quote:
Originally posted by X002548

It would help if you post the actual query...only some of us are mind readers

SELECT * FROM TABLE2 WHERE ID IN (SELECT ID FROM TABLE1)

???

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/




Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-16 : 12:01:24
See...and I even had a blindfold on



Is it Friday yet? Or 5:00?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-02-16 : 13:18:42
Or even 0157?

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-16 : 13:21:28
quote:
Originally posted by DonAtWork

Or even 0157?

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp



Imagine...still being Vertical...amazing



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Davide70
Starting Member

6 Posts

Posted - 2012-02-17 : 03:09:48
Sorry, maybe I have been misunderstood.. that query actually does not work.
The final result shows only the rows with one (random) ID among those listed in the first select.
Any idea??
Thank you

quote:
Originally posted by X002548

See...and I even had a blindfold on



Is it Friday yet? Or 5:00?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/




Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-02-17 : 08:00:43
Try
SET ROWCOUNT  0

Then run your query.

Otherwise, can you show us your ACTUAL query? Like in the HOW TO ASK link?

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -