Sql 与内部连接不同
我有一个问题要问 食用水果Sql 与内部连接不同,sql,Sql,我有一个问题要问 食用水果 FruitID | Fruit | Unit Price 1 | Orange | $3 2 | Apple | $2 3 | Grape | $4 表3详细信息 Picture | Color | FruitID Orange_o.jpg | Orange | 1 Apple_g.jpg | Green | 2 Apple_r.jpg | Red
FruitID | Fruit | Unit Price
1 | Orange | $3
2 | Apple | $2
3 | Grape | $4
表3详细信息
Picture | Color | FruitID
Orange_o.jpg | Orange | 1
Apple_g.jpg | Green | 2
Apple_r.jpg | Red | 2
Grape_p.jpg | Purple | 3
Grape_g.jpg | Green | 3
我想要得到的结果是所有的水果没有重复的名字
范例
Fruit | UnitPrice | Picture | Color
Orange| $3 | Orange_o.jpg | Orange
Apple | $2 | Apple_g.jpg | Green
Grape | $4 | Grape_p.jpg | Purple
这有可能实现吗?
感谢为SQL Server编写的文章,但是实际的查询应该可以在其他数据库上使用 设置数据:
declare @Fruit table (FruitID int not null,Fruit varchar(10) not null,UnitPrice int not null)
insert into @Fruit(FruitID,Fruit,UnitPrice) values
(1,'Orange',3),
(2,'Apple',2),
(3,'Grape',4)
declare @FruitDetails table (FruitID int not null,Picture varchar(20) not null,Color varchar(10) not null)
insert into @FruitDetails (FruitID,Picture,Color) values
(1,'Orange_o.jpg','Orange'),
(3,'Grape_p.jpg','Purple'),
(3,'Grape_g.jpg','Green'),
(2,'Apple_g.jpg','Green'),
(2,'Apple_r.jpg','Red')
查询:
select
f.Fruit,
f.UnitPrice,
fd.Picture,
fd.Color
from
@Fruit f
inner join
@FruitDetails fd
on
f.FruitID = fd.FruitID
left join
@FruitDetails fd_anti
on
f.FruitID = fd_anti.FruitID and
fd_anti.Picture < fd.Picture --This is the condition for picking a better row
where
fd_anti.FruitID is null --This eliminates rows where a better row was picked
这与您预期的结果不匹配,但是您没有为我们提供一个关于从FruitDetail
中选择“最佳”行的条件的良好定义。SQL中没有“选择任何一个”。它不在那里有一个很好的理由;它不是确定性的,这会让您在调试应用程序时非常头疼。然而,手动选择一个是可能的,只需要一些额外的技巧就可以了(有些数据库有扩展,可以使它更简单,但说到通用SQL)
因此,首先您必须选择一个标准,根据该标准,您将选择要加入的一行。假设你想要颜色按字母顺序排在第一位的那个(在真实的情况下,你可能会有这样的优先权或重要性)。您使用的列对于每个水果都必须是唯一的!您可以使用group by或BEY nested select通过另一个联接进行计算。嵌套的select更易于编写和理解。它将是:
(select min(Color) from FruitDetails as inner where inner.Fruit = Fruit.Fruit)
select Fruit.* ThisDetails.*
from Fruit
join FruitDetails as ThisDetails using (Fruit)
join FruitDetails as BestDetails using (Fruit)
group by Fruit.*, ThisDetails.*
having ThisDetails.Color = min(BestDetails.Color)
现在,将表连接到条件中,即颜色为该颜色,因此:
select * from Fruit
join FruitDetails as outer
on outer.Fruit = Fruit.Fruit
and outer.Color = (select min(Color) from FruitDetails as inner where inner.Fruit = Fruit.Fruit)
这假设FruitDetails
表有一个您忘记的Fruit
列,但是如果没有该列,就根本不可能进行连接。它有一个唯一(水果,颜色)
约束,以保证每个水果只有一行具有最小颜色值
具有两个连接的另一个备选方案是:
(select min(Color) from FruitDetails as inner where inner.Fruit = Fruit.Fruit)
select Fruit.* ThisDetails.*
from Fruit
join FruitDetails as ThisDetails using (Fruit)
join FruitDetails as BestDetails using (Fruit)
group by Fruit.*, ThisDetails.*
having ThisDetails.Color = min(BestDetails.Color)
(
using(column)
是大多数(但不是所有)SQL变体中onetable.column=othertable.column
的缩写)您加入的是什么?您现在的查询是什么样子的?如何从水果详细信息中选择正确的行?你怎么知道葡萄是紫色的,苹果是绿色的?我不确定这就是为什么我问这是否可能。这只是一个例子。我用的那个是在家具上。那么有可能实现吗?我将连接两个表,水果和水果细节以生成结果您不能在编写表时连接它们,因为没有列可以连接它们。水果细节需要说明每条线适用于哪种水果。像这样的错误表明您实际上并没有尝试它。使用左连接的缺点是您限制了查询优化器。通过嵌套选择或其他分组连接选择“最佳”图片限制较少。@JanHudec-如果查找“最佳”结果行需要复杂的多列条件,我可以用反连接表示,但不能用嵌套选择表示。是的,如果无法计算表示顺序的数字或值,则嵌套选择,也不能使用分组联接。