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
 Finding the results of combining 2 sql queries

Author  Topic 

velnias2010
Posting Yak Master

125 Posts

Posted - 2011-02-02 : 04:18:22
lets say you have 2 queries

select content_id from tblTest where categoryId = 10

select content_id from tblTest where categoryId = 5

And I want the content_id which are in both only ?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-02-02 : 04:44:51
select content_id from tblTest t1 where categoryId = 10
and exists(select * from tblTest t2 where t2.categoryId = 5 and t2.content_id = t1.content_id)




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-02 : 08:53:07
If you use versions starting from 2005,

select content_id from tblTest where categoryId = 10
INTERSECT
select content_id from tblTest where categoryId = 5

Madhivanan

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

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2011-02-03 : 01:31:40
select content_id from tbltest where content_id in(10,5)
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-03 : 01:42:01
quote:
Originally posted by ahmeds08

select content_id from tbltest where content_id in(10,5)



I think the OP is looking for contents that have both categories 5 AND 10 ?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-03 : 03:00:32
quote:
Originally posted by MIK_2008

quote:
Originally posted by ahmeds08

select content_id from tbltest where content_id in(10,5)



I think the OP is looking for contents that have both categories 5 AND 10 ?


Yes it is. It is not simple as what ahmeds08 posted

Madhivanan

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

- Advertisement -