如何使用一个id将四个表连接在一起

时间:2017-11-28 20:25:16

标签: html sql

我有四个表,我想使用SQL查询加入它们并在产品页面上显示产品

Table 1 [table name: A] has product_id and category_id, 
Table 2 [table name: B] has product_id, and our_price,  
Table 3 [table name: C] has product_id, and product_name  
Table 4 [table name: D] has product_id, description, and saving_price.

在这里,我想

join Table A, B, C, and DSELECT all product's数据from tables B, C, D which category_id = 77 in table A

我想在产品页面上打印product_name, description, saving_price, and our_price,其类别ID为77.

1 个答案:

答案 0 :(得分:0)

大概这就是你想要的,虽然很难从你问的方式知道

SELECT
    A.product_id,
    A.category_id,
    B.our_price,
    C.product_name,
    D.description,
    D.saving_price
FROM
    A
    INNER JOIN B ON A.product_id = B.product_id
    INNER JOIN C ON A.product_id = C.product_id
    INNER JOIN D ON A.product_id = D.product_id
WHERE
    A.category_id = 77

以上假设所有表中都存在数据。如果您在某些数据中缺少数据,则可能需要使用LEFT OUTER JOIN