15

如何使用INNER JOIN从第二个和第三个表中选择某些列

SELECT 
    *
FROM
    1_packages_plu AS p
        INNER JOIN
    1_stock as s ON p.fk_products_id = s.fk_products_id
        AND branch = 1
        INNER JOIN
    1_products AS j ON p.fk_products_id = j.id
WHERE
    fk_packages_id = 54;

在表格中1_stock,我只想返回stock-repair列的值,而1_products我只需要制作、模型列

4

3 回答 3

30
SELECT
    p.* -- All columns from p
    ,
    s.columnName -- Just that column from s
    ,
    j.columnName -- And just that column from j

FROM
    1_packages_plu AS p

    INNER JOIN 1_stock as s
        ON p.fk_products_id = s.fk_products_id
        AND
        branch = 1

    INNER JOIN 1_products AS j
        ON p.fk_products_id = j.id

WHERE
    fk_packages_id = 54
于 2012-12-28T11:27:29.853 回答
4

您需要 (.) 运算符来访问列:

SELECT 
    p.* , 
    s.stock-repair, 
    j.make, j.model
FROM
    1_packages_plu AS p
        INNER JOIN 1_stock as s 
          ON p.fk_products_id = s.fk_products_id
               AND branch = 1
        INNER JOIN 1_products AS j 
          ON p.fk_products_id = j.id
WHERE
    fk_packages_id = 54

ORDER BY p.colunmname 
;
于 2012-12-28T11:29:24.490 回答
2
SELECT Table1.*, Table2.FK, Table2.SomeColumn, Table2.SomeColumn, Table3.SomeColumn, Table3.SomeColumn
FROM   
Table1 INNER JOIN
Table2 ON Table1.FK = Table2.Table1FK INNER JOIN
Table3 ON Table2.FK = Table3.Table2FK
于 2012-12-28T11:36:54.500 回答