views:

46

answers:

2

I've been looking into JOIN, subqueries and other ways of doing this, but I can't work out the best way to do this is...

I have a table (ps_category_product): id_product, id_category

I want to perform a query on it like: SELECT id_product FROM ps_category_product WHERE id_category='$this_cat'

BUT, I only want to perform this query where the ID's are returned by a query on another table (ps_product): id_product, active

SELECT id_product FROM ps_product WHERE active='1'

Can anyone help me with getting these two queries working together?

A: 

Use like this,

SELECT a.id_product FROM ps_category_product as a, ps_product as b WHERE a.id_category='$this_cat' and a.id_product = b.id_product and b.active='1'

Karthik
+4  A: 

What about something like this:

SELECT pp.id_product FROM ps_category_product pcp
INNER JOIN ps_product pp ON pp.id_product = pcp.id_product
WHERE pp.active = '1'
Phill Sacre
I'm getting:Query failed: Column 'id_product' in field list is ambiguous
Chris Harrison
include the table name to the field - pp.id_product
Riho
Thanks - updated to fix
Phill Sacre
Thanks Phill and Riho, where would I put the WHERE clauses for the id_category='$this_cat'? Wherever I insert it I get errors.
Chris Harrison
@Chris - you need to add it on at the end, like this: WHERE pp.active = '1' AND id_category = '$this_cat'It would be well worth reading a book on SQL, this one seems to be pretty good: http://www.amazon.com/Beginning-SQL-Programmer-Paul-Wilton/dp/0764577328/ref=cm_lmf_tit_1
Phill Sacre
Thank you to all those who helped with this question.
Chris Harrison