MySQL - JOIN 3 Rows in 1 Table to 1 Row in another Table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shellfish
Forum Newbie
Posts: 2
Joined: Wed Feb 27, 2008 8:44 pm

MySQL - JOIN 3 Rows in 1 Table to 1 Row in another Table

Post by Shellfish »

Hi there, Hoping someone can help me. I need to create a recordset which Joins 3 rows from one table on 1 row from another table.

At present I have:

SELECT * FROM tb_product_sub_cat
LEFT JOIN tb_products ON tb_product_sub_cat.category_id = tb_products.product_subcategory
WHERE tb_product_sub_cat.category_name = 'stuff'

I also need:

tb_product_sub_cat.category_id = tb_products.product_subcategory2 AND
tb_product_sub_cat.category_id = tb_products.product_subcategory3

Is there a join which can do an AND for the JOINS?
I have a table of Product Sub-Categories, and a Products table with product_subcategory, product_subcategory2 & product_subcategory3
I need to find all products that have 'stuff' in either/or all of the product categories.
Thanks in advance, and I hope it's clear enough to understand

Regards, Warren
Iainzor
Forum Newbie
Posts: 4
Joined: Mon Oct 26, 2009 10:54 am

Re: MySQL - JOIN 3 Rows in 1 Table to 1 Row in another Table

Post by Iainzor »

JOINS can have multiple conditions:

Code: Select all

 
SELECT * FROM tb_product_sub_cat 
    LEFT JOIN tb_products ON 
        tb_product_sub_cat.category_id = tb_products.product_subcategory AND
        tb_product_sub_cat.category_id = tb_products.product_subcategory2 AND
        tb_product_sub_cat.category_id = tb_products.product_subcategory3
WHERE tb_product_sub_cat.category_name = 'stuff'
 
Post Reply