|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
(seemingly) simple SQL SELECT statement
Hey all,
I admit, I'm very bad with SQL. I'm trying to do something, that I would think would be easy, but I cannot get it right. I know this has to be" SQL 101", but, I've stuck. Here's the scoop. 3 tables. products -------- products_id products_number (other stuff)... categories ---------- categories_id categories_name (other stuff)... products_x_categories ---------------------- products_id categories_id How would I got about getting the products data from the table 'products' that have been assigned to two different categories (assigned to BOTH categories)? Example data... products -------- 1 MMT100 2 MMT200 3 XXT100 4 XXT200 categories ---------- 1 "Foo" 2 "Bar" 3 "Goo" products_x_categories --------------------- 1 1 1 2 2 1 2 3 3 1 3 2 4 3 Query = "Give me the products id and name that belong to BOTH 'foo' and 'bar'; result = 1 MMT100 3 XXT100 So, how would I write that SQL? Cheers! Fozzy *edit* spelling/grammer |
|
#2
|
|||||
|
|||||
|
I think you want something like this:
MySQL Code:
Didn't test for syntax errors, so it may not work out of the box.
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 263
![]() |
|
#3
|
|||
|
|||
|
Thanks for the response Itsacon,
I don't think that's exactly what I'm looking for. It appears there's a condition that is (2 == ...) which, in my case, can me anything more than two. I did find another solution, which required using an extra field as a check, which wasn't used before. Thanks for the help mate! Cheers, Fozzy |
|
#4
|
||||
|
||||
|
the "2 ==" can of course be replaced by "1 <", so you get everything with two or more entries.
|
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > (seemingly) simple SQL SELECT statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|