MySQL Inner Join Query Multiple Tables

后端 未结 2 678
星月不相逢
星月不相逢 2020-11-29 07:36

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

相关标签:
2条回答
  • 2020-11-29 08:20

    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 |
    
    0 讨论(0)
  • 2020-11-29 08:33

    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';
    
    0 讨论(0)
提交回复
热议问题