多表联合查询条件

Join query for multiple tables with condition

我正在使用 MSsql,但在尝试从 SELECT 查询中获取结果时遇到了困难。我有 3 tables.

(有关客户的数据 - 买家和卖家)。

select * from Product;

id(PK) | name_product
----------------------
1      | apple
2      | orange
3      | juice

select * from Seller;

id_seller(PK) | id_product | product_placement_date
---------------------------------------------------
45            | 3          | 2020-01-09
46            | 3          | 2020-01-05
58            | 2          | 2020-02-08
49            | 2          | 2020-01-04
43            | 1          | 2020-01-06

select * from Customer;

id_customer(PK) | name_customer
---------------------------
43          | Alice
45          | Sam
46          | Katy
49          | Soul
58          | Fab

我正在寻找 select 产品名称和放置该产品的第一个卖家(给定第一个放置日期)。

我试过这个:

SELECT  C.name_product,
        P.mindate,
        P.name_customer
FROM Product AS C
    CROSS APPLY(SELECT MIN(S.product_placement_date) as mindate,
                            T.name_customer
                FROM Seller AS S
                JOIN Customer AS T ON T.id_customer = S.id_seller
                WHERE S.id_product = C.id) AS P

但我没有得到正确的结果。我想要如下所示的结果:

name_product | product_placement_date | name_customer
-----------------------------------------------------
apple        | 2020-01-06             | Alice
orange       | 2020-01-04             | Soul
juice        | 2020-01-05             | Katy

请指教

    SELECT  P.name_product,
        S.product_placement_date,
        S.name_customer
FROM Product AS P
    CROSS APPLY(SELECT TOP 1 S.product_placement_date,
                            C.name_customer
                FROM Seller AS S
                    INNER JOIN Customer AS C ON C.id_customer = S.id_seller
                WHERE S.id_product = P.id
                ORDER BY S.product_placement_date) AS S

看起来您可能与卖家有问题 table。卖家 ID 似乎是客户 table 的外键。这将表明您永远不会允许卖家在任何其他日期出售任何其他商品……除非 table 的主键是卖家 ID、所售商品和日期,从而提取所有 3 列。我希望“卖家”table 真的是一个“卖家”table 并且更符合

的背景
SellingID (PK) | id_seller | id_product | product_placement_date
---------------------------------------------------
1              | 45            | 3          | 2020-01-09
2              | 46            | 3          | 2020-01-05
3              | 58            | 2          | 2020-02-08
4              | 49            | 2          | 2020-01-04
5              | 43            | 1          | 2020-01-06

接下来要考虑的是,如果两个或更多人在同一天出售橙子并上市会怎样。在您的原件上,您不知道谁首先列出了他们的产品……或者您是否希望所有最早列出其产品的人。其中你可以同时显示两个名字。通过将此“selling”table 与“sellingid”列作为自动增量,您将能够根据给定产品的最早 SELLINGID 知道谁是第一个,因为有人必须提交他们的记录首先,即使在同一天。然后你可能会得到类似

的结果
select
      p.name_product,
      S2.product_placement_date,
      c.name_customer
   from
      ( select id_product,
               min( sellingid ) as FirstListedID
           from
              selling
           group by
              id_product ) First
         join selling S2
            on First.FirstListedID = s2.sellingID
            join customer c
               on S2.id_seller = c.id_customer
            join product p
               on S2.id_product = p.id

在这里,销售 activity 到别名“First”的预查询表示所有产品的单个列表,其中包含第一个销售 ID 实例的销售日期,而不管根据解释原因和使用自动-在同一日期多人报价的情况下增加。

完成后,重新加入第一个“ID”上的原始销售 table。然后你就可以加入产品和客户了解最后的细节了。