troubleshooting Question

SQL Query help (AND across rows)

Avatar of Si_Hibbard
Si_HibbardFlag for United States of America asked on
Microsoft SQL ServerMySQL ServerSQL
5 Comments1 Solution184 ViewsLast Modified:
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.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros