简体   繁体   中英

Left Join Specific Columns From Two Different Tables Using Alias

I have two tables:

toys

item_id int(4) unsigned

item_desc varchar(100)

initial_quantity int(4) unsigned

price decimal(5,2)

and

toy_purchases

item_id int(4) unsigned

customer_name varchar(100)

quantity int(4) unsigned

purchase_date date

Only using aliases, how would I list out only the item description from the toys table, along with only the customer_name and purchase_date (whether or not they exist) from toy_purchases.

I have tried the following:

select b.book_name 
from books as b 
left outer join bc.customer_name, bc.purchase_date 
from book_customers as bc on b.bookid=bc.itemid; 

That is not the proper syntax for a query. I think you are actually after:

SELECT t.item_desc AS Description, 
       tp.customer_name AS Customer, 
       tp.purchase_date AS DatePurchased 
FROM toys t 
LEFT JOIN toy_purchases tp ON t.item_id = tp.item_id

To left join table1 with only specific columns from table2 using an alias we can do this:

            SELECT 
              A.*, B.aThingIWant1, B.aThingIWant2
            FROM table1 A

            LEFT JOIN table2 B
            ON A.id = B.application_id
        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM