Welcome to WebmasterWorld Guest from 184.108.40.206 , register , free tools , login , search , pro membership , help , library , announcements , recent posts , open posts Become a Pro Member
MYSQL Join problem Elric99 msg:4077209 9:05 pm on Feb 9, 2010 (gmt 0) Hello, 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.
Tommybs msg:4077235 10:03 pm on Feb 9, 2010 (gmt 0)
Hi, Have you tried the following <code> 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') </code> Is that the kind of thing you're after? Tommybs msg:4077236 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
Elric99 msg:4077570 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.