Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INNER JOIN select columns from table2

Tags:

mysql

How can I select certain columns from the second and third tables using 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_stock I only want to return the value of stock-repair columns and in the 1_products all I need is make,model columns

like image 436
Stephen Bouffe Avatar asked Dec 28 '12 11:12

Stephen Bouffe


2 Answers

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
like image 87
scragar Avatar answered Oct 03 '22 07:10

scragar


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 
;
like image 4
Grijesh Chauhan Avatar answered Oct 03 '22 06:10

Grijesh Chauhan