简体   繁体   English

SQL左联接-内部选择不返回列

[英]SQL LEFT JOIN - Inner select not returning columns

I have two tables called 'Customers' and 'Orders'. 我有两个表,分别称为“客户”和“订单”。 Tables column names are as follow: 表的列名称如下:

Customers: id, name, address
Orders: id, person_id, product, price 

The desired outcome is to query all customers with one of their latest purchases. 理想的结果是向所有客户查询其最新购买的商品之一。 I have a lot of duplicates in 'Orders' table whereby two records with same time-stamp due to some bug. 我在“订单”表中有很多重复项,由于某些错误,两条记录具有相同的时间戳。

I have written the following code but the issue is that the query does not return table 2(Orders) column values. 我已经编写了以下代码,但问题是查询不返回表2(Orders)列值。 Can anyone advise what the issue is? 谁能告诉我这个问题是什么?


SELECT C.Id,C.Name, O.item, O.price, O.product
FROM Customers C
LEFT JOIN
    (
        SELECT TOP 1 person_id
        FROM Orders 
        WHERE status = 'Pending'
    ) O ON C.ID = O.person_id

Results:  O.item, O.price, O.product values are all null 

Edit: Sample Data 编辑:样本数据

ID/ NAME/ ADDRESS/ 身份证/姓名/地址/
1/ A/ Ad1/ 1 / A /广告1
2/ B/ Ad2/ 2 / B / Ad2 /
3/ C/ Ad3/ 3 / C / Ad3 /

ID/ Person ID/ PRODUCT PRICE/ Created Date ID /人员ID /产品价格/创建日期
ID-1234/ 1/ Book/ $5/ 26-2-2017 ID-1234 / 1 /书本/ $ 5 / 26-2-2017
ID-1235/ 1/ Book/ $5/ 26-2-2017 ID-1235 / 1 /书本/ $ 5 / 26-2-2017
ID-1236/ 2/ Calendar/ $10/ 4-2-2017 ID-1236 / 2 /日历/ $ 10 / 4-2-2017
ID-1238/ 1/ Pen/ $2/ 1-1-2016 ID-1238 / 1 /笔/ $ 2/2016年1月1日

Assuming that the id column in Orders is a primary key autoincrement, then the following should work: 假设“ Orders中的id列是主键自动递增,则应执行以下操作:

SELECT c.id,
       c.name,
       COALESCE(t1.price, 0.0) AS price,
       COALESCE(t1.product, 'NA') AS product
FROM Customers c
LEFT JOIN Orders t1
    ON c.id = t1.person_id
LEFT JOIN
(
    SELECT person_id, MAX(CAST(SUBSTRING(id, 4, LEN(id)) AS INT)) AS max_id
    FROM Orders
    GROUP BY person_id
) t2
    ON t1.person_id = t2.person_id AND
       t2.max_id    = CAST(SUBSTRING(t1.id, 4, LEN(t1.id)) AS INT)

This answer assumes that taking the greatest order ID per customer will yield the most recent purchase. 该答案假设采用每个客户的最大订单ID将产生最近的购买。 Ideally you should have a timestamp column which captures when a transaction took place. 理想情况下,您应该有一个timestamp列,该列捕获事务发生的时间。 Note that even in the query above, we still have no way of knowing when the most recent transaction took place. 需要注意的是,即使在上面的查询,我们仍然不知道最近交易发生的方式。

So where is the timestamp column? 那么timestamp列在哪里? It's not mentioned in your table schema. 您的表架构中未提及。 But your description does not mention the status column either, and that is clearly in there. 但是您的描述也没有提到status栏,并且显然在其中。
Is orders.id unique? orders.id是否唯一? Is it the key for the Orders table?> If it is, then your schema has no way to identify "duplicate" records. 它是Orders表的键吗?>如果是,则您的架构无法识别“重复”记录。 You cannot mean to imply that only one order per customer is allowed, so if there are multiple orders for a single customer, how do we identify the duplicates? 您不能表示每位客户只允许一个订单,因此如果单个客户有多个订单,我们如何识别重复的订单? By the unmentioned timestamp column? 通过未提及的timestamp列?

If there IS a `timestamp column, and that's how you would identify dupes, then use it. 如果有一个“时间戳记”列,这就是您识别重复项的方式,请使用它。

SELECT C.Id,C.Name, O.item, O.price, O.product
FROM Customers C LEFT JOIN Orders o
   on o.id = (Select Min(id) from orders
              where person_id = c.Id
                and timestamp = o.timestamp
                and status = 'Pending')

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

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