使用别名从两个不同的表左连接特定列

时间:2014-07-30 17:48:18

标签: mysql sql join alias

我有两张桌子:

玩具

ITEM_ID int(4)unsigned

ITEM_DESC VARCHAR(100)

initial_quantity int(4)unsigned

价格 十进制(5,2)

toy_purchases

ITEM_ID int(4)unsigned

CUSTOMER_NAME VARCHAR(100)

量 int(4)unsigned

PURCHASE_DATE 日期

仅使用别名,我如何仅从玩具表中列出商品描述,以及仅从toy_purchases中列出customer_name和purchase_date(无论它们是否存在)。

我尝试了以下内容:

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; 

1 个答案:

答案 0 :(得分:1)

这不是查询的正确语法。我想你实际上是在追求:

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