我有一张表,上面有一个所有者的匹配衣服类型和模型名称。该表由我使用的软件提供,我无法更改模型的设计。
这个衣橱的主人是保罗,他的个人 ID 是 poa
所有者只能拥有每种类型的衣服。如果 Date_own_to
是 31-12-2099
,这意味着所有者没有把衣服带回来。
每种类型对应一个表,其中包含有关产品的更多信息
这是鞋子的例子:
这是衬衫:
对于套接字:
我想做的是像这样列出每个人的每件衣服:
所以我试着这样做:
SELECT * FROM wardrobe w
JOIN shoes sho
on sho.clothes_id = w.cid
where owner_id = 'poa'
根据这个请求,我得到了这个:
但是现在当我尝试用这个请求加入另一个表时
SELECT * FROM wardrobe w
JOIN shoes sho
on sho.clothes_id = w.cid
JOIN shirt shi
on shi.clothes_id = w.cid
where owner_id = 'poa'
结果是 0 行。
我不知道如何正确加入值。
我做错了什么?
多行
因为我在一段时间内只拥有一件 T 恤。如果我想查找某个人在给定时间段内拥有的衣服,我想做这样的事情:
date_own_from = '01-01-2021' 和 date_own_to = '31-12-2021'
所以期望的结果是:
当拥有鞋子的人拥有 Shirt 和 Socket 时,当年拥有的第 2 行不同的鞋子。
最佳答案
考虑到你必须使用的糟糕设计,你在这里会遇到困难。
这里是示例数据和 ddl,以防有人想一起玩。
create table Wardrobe
(
ID int
, owner_id varchar(10)
, cid varchar(10)
, Name varchar(20)
, Type varchar(10)
)
insert Wardrobe
select 1, 'poa', 'sho-1', 'SnakeSpeed', 'Shoes' union all
select 2, 'poa', 'shi-1', 'SimpleT', 'Shirt' union all
select 3, 'poa', 'so-21', 'White model', 'Sockets'
insert Wardrobe
select 4, 'poa', 'so-22', 'green', 'Sockets'
create table Shoes
(
ID int
, Clothes_ID varchar(10)
, Size varchar(10)
)
insert Shoes
select 1, 'sho-1', '42'
create table Shirts
(
ID int
, Clothes_ID varchar(10)
, Size varchar(10)
)
insert Shirts
select 1, 'shi-1', 'M'
create table Sockets
(
ID int
, Clothes_ID varchar(10)
, Size varchar(10)
)
insert Sockets
select 2, 'so-22', 'M'
可能还有其他一些方法可以解决这个问题。但是,即使一个人只有两只鞋和一件没有 socket 的衬衫,这个例子也能奏效。我首先使用一些 ctes 和 row_number 隔离每组行。然后通过一些条件聚合将它们组合在一起。
declare @Owner varchar(10) = 'poa';
with cteShoes as
(
select w.ID
, w.owner_id
, w.Name
, sho.Size
, RowNum = ROW_NUMBER() over(order by w.Name)
from Wardrobe w
join Shoes sho on sho.Clothes_ID = w.cid and w.Type = 'Shoes'
where w.owner_id = @Owner
)
, cteShirts as
(
select w.ID
, w.owner_id
, w.Name
, shi.Size
, RowNum = ROW_NUMBER() over(order by w.Name)
from Wardrobe w
join Shirts shi on shi.Clothes_ID = w.cid and w.Type = 'Shirt'
where w.owner_id = @Owner
)
, cteSockets as
(
select w.ID
, w.owner_id
, w.Name
, soc.Size
, RowNum = ROW_NUMBER() over(order by w.Name)
from Wardrobe w
join Sockets soc on soc.Clothes_ID = w.cid and w.Type = 'Sockets'
where w.owner_id = @Owner
)
select Owner = coalesce(sho.owner_id, shi.owner_id, soc.owner_id)
, Shoes = max(case when sho.RowNum is not null then sho.Name end)
, ShoesSize = max(case when sho.RowNum is not null then sho.Size end)
, Shirt = max(case when shi.RowNum is not null then shi.Name end)
, ShirtSize = max(case when shi.RowNum is not null then shi.Size end)
, Sockets = max(case when soc.RowNum is not null then soc.Name end)
, SocketsSize = max(case when soc.RowNum is not null then soc.Size end)
from cteShoes sho
full outer join cteShirts shi on shi.RowNum = sho.RowNum
full outer join cteSockets soc on soc.RowNum = sho.RowNum
group by coalesce(sho.ID, shi.ID, soc.ID)
, coalesce(sho.owner_id, shi.owner_id, soc.owner_id)
关于sql - 为什么我用一个个人id加入多个表时得到0行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69436695/