INNER JOIN从table2中选择列

时间:2012-12-28 11:25:44

标签: mysql

如何使用 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我需要的是make,model columns

3 个答案:

答案 0 :(得分:21)

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

答案 1 :(得分: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 
;

答案 2 :(得分:0)

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