MySQL INNER JOIN 从 table2 中选择列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14068780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:57:20  来源:igfitidea点击:

INNER JOIN select columns from table2

mysql

提问by Stephen Bouffe

How can I select certain columns from the second and third tables using INNER JOIN

如何使用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;

In the tables 1_stockI only want to return the value of stock-repaircolumns and in the 1_productsall I need is make,model columns

在表中,1_stock我只想返回stock-repair列的值,而1_products我只需要 make,model 列

回答by scragar

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

回答by Grijesh Chauhan

You need (.) operator to access column:

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

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 
;

回答by sanyam9999

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