homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

MYSQL Join problem

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


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)


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)

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)

Hi Tommybs,

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved