0

I have a table where I store purchases, and a table where I store products.

Each row in the purchases table can contain up to 9 products ids (8 can be empty)

id, foo, bar, baz, product_1_id, product_2_id, product_3_id, etc... 

Now I need to query all the fields of that purchases table, to display in a html page.

I can't display only the products ids, so I need to find the corresponding product_name in the products table.

The SQL query is giving me a hard time.

Any help would be really appreciated.

5
  • 2
    Shouldn't you just have a products table, instead of limiting a purchase to nine products? Commented Jul 31, 2012 at 13:51
  • Can you redesign the database to use a join table? Commented Jul 31, 2012 at 13:52
  • 1
    "Each row in the purchases table can contain up to 9 products ids (8 can be empty)". Typically this would be stored in a separate table, "purchase_item" or "purchase_product". I guess "purchase" is not really a good name since that refers to one individual item. Classic names are "purchase_order" and "purchase_order_item" or "purchase_order_line". The problem with your design is that it's not flexible (only up to 9 products per purchase) and that it is hard to query. You'll need a join for each potential product per purchase. With a separate purchase_order_item table you need just one. Commented Jul 31, 2012 at 13:53
  • I wonder what message you'll send to the user when he/she adds a 10th product to the order. "Sorry but we want to sell you only 9 items?" :) Commented Jul 31, 2012 at 14:01
  • Your comments are right, I must admit. BUT, if someone ever wants to buy ten products at once, I'd be happy to rethink everything from scratch, free of charge... ;) Commented Jul 31, 2012 at 14:22

6 Answers 6

2

To retrieve the product names, I'd try this:

SELECT t2.product_name as product_name_1, t3.product_name as product_name_2, t4.product_name as product_name_1, [...]
FROM purchases as t1
JOIN products as t2 ON(t1.product_1_id = t2.product_id)
LEFT JOIN products as t3 ON(t1.product_2_id = t3.product_id)
LEFT JOIN products as t4 ON(t1.product_3_id = t4.product_id)
[...]
WHERE t1.id = ...

but this will probably be VERY slow, you might want to restructure your database.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to join with Products table 9 times. But consider normalisation

select id, foo, bar, baz, p1.product_name, p2.product_name, p3.product_name, 
  etc...  
from purchases as pur
left join products as p1 on pur.product_1_id=p1.product_id
left join products as p2 on pur.product_2_id=p2.product_id
.
.

Comments

0

in sql you can use like this..

 select *,PM.Product_id,P.Product_name from purchase_mst PM, product P where   
 PM.Product_id1=P.Product_id1 and PM.Product_id2=P.Product_id2 etc....

Comments

0

With a properly normalized design you'd have: product, purchase_Order and purchase_order_line and the query would simply be:

SELECT     ...
FROM       purchase_order
INNER JOIN purchase_order_line
ON         purchase_order.id = purchase_order_line.purchase_order_id
INNER JOIN product
ON         purchase_order_line.product_id = product.id

3 Comments

Thank you Roland. I'd love to learn. Where can I find informations about properly normalized designs ?
there is a lot of stuff on the web, just google it. For example, this is a reasonable start databases.about.com/od/specificproducts/a/normalization.htm and so is this dreamincode.net/forums/topic/…
dreamincode.net/forums/topic/… is really helpful. Thanks Roland.
0

I think it would be easier if you had a table connecting purchases and products with two columns *product_id, purchase_id*.

But still, I guess you can try something like this :

SELECT pr1.name
 FROM purchases pur1, products pr1
WHERE pur1.product_1_id = pr1.id
UNION ALL
SELECT pr2.name
 FROM purchases pur2, products pr2
WHERE pur2.product_1_id = pr2.id
UNION ALL
-- and so on until
SELECT pr9.name
 FROM purchases pur9, products pr9
WHERE pur9.product_9_id = pr9.id

Although this looks pretty scary and I would strongly suggest rethinking your database design.

Comments

0

You could do this

Select purchase., product. from purchase join product on id1=id, id2=id... Etc.

Read this:

MySQL how to join tables on two fields

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.