Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL JOIN Question 1

Status
Not open for further replies.

jeff5311

Programmer
Mar 1, 2002
31
US
This query I'm trying to construct seems simple, but I can't get it to work. Can someone please help?!?!?!

I have two tables...

My "products_to_traits" table has the following data:

product_num trait_num
1 1
1 12
1 23
1 30
2 1
2 30
2 43


My "products" table has the following data:

product_num product_name
1 Widget
2 Thingamabob


How would I create a query that would show only the products that have the trait_num's 30 & 12 ("widget")?

Thanks in advance!!!
Jeff
 
? is the trait number.
Code:
SELECT * FROM products AS p LEFT JOIN traits AS t ON (p.product_num = t.product_num) WHERE t.trait_num = ?

M. Brooks
 

another fine example of the "red/blue car problem"
Code:
select product_name
  from products
inner
  join products_to_traits
    on products_to_traits.product_num
     = products.product_num
 where trait_num in ( 30, 12 )
group
    by product_name
having count(distinct trait_num) = 2
if the combination of product_num and trait_num in the products_to_traits table is unique (as it would be if declared as the PK for that table), then you don't need DISTINCT in the COUNT




r937.com | rudy.ca
 
Thanks for your help guys!

r397 - that's exactly what I was looking for... it works perfectly.

Thanks Again!
Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top