12

I am trying to join up a few tables and examples of the layouts are below:

orders

user_id=7 pricing id=37

products_pricing

id=37 product_id=33

products

id=33 name=test product

SQL

SELECT *
FROM orders
  INNER JOIN products_pricing
    ON orders.pricing_id = products_pricing.id
  INNER JOIN products
    ON products_pricing.product_id = products.id
WHERE orders.user_id = '7' ");

listings

id=233 user_id=7 url=test.com

With this SQL i get an output giving me all the products from the user_id of 7 and it will list each products name in a while loop. However when I add another INNER JOIN for a table called listings, which has a user_id column and I need to grab a url for each row that matches so I can hyperlink the product names with the url I get everything contained in the listings table as well as the working stuff above. I'm either doing it very wrong or am missing something. I've spent a few hours trying to figure it out but keep getting the same result. Can anyone help me out?

1
  • instead of INNER JOIN use LEFT JOIN Feb 3, 2013 at 14:21

2 Answers 2

27

Try this:

SELECT 
  p.id,
  p.name,
  l.url,
  o.user_id,
  o.pricing_id
FROM orders AS o
INNER JOIN products_pricing AS pp ON o.pricing_id  = pp.id
INNER JOIN products         AS  p ON pp.product_id = p.id
INNER JOIN listings         AS  l ON l.user_id = o.user_id
WHERE o.user_id ='7' 
  AND l.id = 233 
  AND l.url = 'test.com';

SQL Fiddle Demo

For the sample data you posted in your question, this will give you:

| ID |        NAME |      URL | USER_ID | PRICING_ID |
------------------------------------------------------
| 33 | testproduct | test.com |       7 |         37 |
3
  • Mahmoud,Thank you for taking the time to help me. This works however I seem to be getting 7 results based on the query and six of them are duplicates I ran the sql in phpmyadmin, any ideas what could cause that?
    – Daniel
    Feb 3, 2013 at 14:56
  • @Daniel Add the keyword DISTINCT : SELECT DISTINCT p.id, p.name, ... this duplicates might because of the INNER JOIN. Could you please add some sample data for those duplicates in the demo. IT will be helpful., Feb 3, 2013 at 15:01
  • I did some more testing, it appears that if you have all the same product for one user it displays them nice without duplicates. However if you add another product to the user it will then get confused to which data belongs to correct output, thus producing the correct result and then a result for other product using the firsts details. Its abit strange.
    – Daniel
    Feb 3, 2013 at 15:55
2

Yes this can be done using the INNER join itself.and fetch select column in select statement.

SELECT 
  p.id,
  p.name,
  l.url,
  o.user_id,
  o.pricing_id
FROM orders AS o
INNER JOIN products_pricing AS pp ON o.pricing_id  = pp.id
INNER JOIN products         AS  p ON pp.product_id = p.id
INNER JOIN listings         AS  l ON l.user_id = o.user_id
WHERE o.user_id ='7' 
  AND l.id = 233 
  AND l.url = 'test.com';
2
  • 10
    Is this a copy of the above answer?
    – Matthew
    Feb 3, 2013 at 14:48
  • Yes i added fetch select column in select statement Feb 3, 2013 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.