简体   繁体   English

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

[英]Left Join Specific Columns From Two Different Tables Using Alias

I have two tables:我有两张桌子:

toys玩具

item_id int(4) unsigned item_id int(4) 无符号

item_desc varchar(100) item_desc varchar(100)

initial_quantity int(4) unsigned initial_quantity int(4) 无符号

price decimal(5,2)价格小数(5,2)

and

toy_purchases玩具购买

item_id int(4) unsigned item_id int(4) 无符号

customer_name varchar(100)客户名称 varchar(100)

quantity int(4) unsigned数量 int(4) 无符号

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.仅使用别名,我将如何仅列出玩具表中的项目描述,以及来自 toy_purchases 的 customer_name 和 purchase_date(无论它们是否存在)。

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:要使用别名仅使用 table2 中的特定列连接 table1,我们可以这样做:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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