Welcome to WebmasterWorld Guest from

Forum Moderators: open

MYSQL Join problem

9:05 pm on Feb 9, 2010 (gmt 0)

5+ Year Member


Here's a problem that I've been wrestling with and googling for about 12 hours now. I hope I can explain myself adequately.

I have two tables set up like this:

table: products

| product_name | brand_name |
| product a | company x |
| product b | company y |

table: tags

| product_name | tag |
| product a | feature 1 |
| product a | feature 2 |
| product b | feature 2 |

So table 'tags' contains features about the products while 'products' contains other information on the products. Unfortunately I can't have the two in one table.

What I'm struggling with is making an SQL call which says something like:

"join the tables and show all products which have feature 1 and feature 2"

Here's what I've been trying:

SELECT tags.product_name, brand_name, tag
FROM products JOIN tags
ON products.product_name = tags.product_name
WHERE tag = 'feature a' AND tag = 'feature b'
ORDER BY product_name ASC

The problem I have is I can't find how to choose TWO columns from the tags table.

I hope I've explained my problem and thank you for any help you can give a tired coder.
10:03 pm on Feb 9, 2010 (gmt 0)

5+ Year Member


Have you tried the following
SELECT pt.product_name, p.brand_name, pt.tag FROM products as p INNER JOIN tags as pt ON p.product_name = pt.product_nae
WHERE pt.tag IN('feature a','feature b')

Is that the kind of thing you're after?
10:04 pm on Feb 9, 2010 (gmt 0)

5+ Year Member

Note: I've made a typo in the above on pt.product_nae should be product_name
12:49 pm on Feb 10, 2010 (gmt 0)

5+ Year Member

Hi Tommybs,

Thanks for the posts. I've got that sorted with your help. Much appreciated.

Featured Threads

Hot Threads This Week

Hot Threads This Month