We help IT Professionals succeed at work.
Get Started

SQL Query help (AND across rows)

182 Views
Last Modified: 2010-03-20
Hi all,
Need some help with this.
3 tables - Tag_Table, Media_Table, TagXMedia_Table

TagID is primary key of Tag_Table and foreign key in TagXMedia_Table
MediaID is a primary key of MediaTable and a foreign key of  TagXMedia_Table

So i can have multiple tags reference multiple media items.

What i want to be able to do is select all the mediaId's from the TagXMedia_Table which have a TagId of x AND x AND x etc

So I am trying to select single MediaID's which have all of the given TagIds - like an AND select statement but accross the rows. I cant do the simple select * from TagXMedia_Table where TagID=1 AND TagID=2 for example as i have actually got:

TagID   |    MediaID
----------------------
1          |      40
1          |      59
2          |      40

and i want to return 40 in this case.

Hope this makes some sense.

Comment
Watch Question
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE