假设我有 3 张桌子:
-
Cars
-
汽车颜色历史
-
Color:
我想选择所有汽车及其颜色,但重要的是,汽车的颜色是最后修改的颜色CarColorHistory
table.
我需要使用 join 来做到这一点。
Example:
Cars:
1
2
汽车颜色历史:
1 1 1 26/03/2012 -> (actual color, can be take by date or id)
2 1 2 25/03/2012
3 2 2 25/03/2012
Color:
1 Blue
2 Red
我需要得到结果:(汽车 ID,颜色名称)
1 Blue
2 Red
我尝试通过加入 Cars 表和 CarColorHistory 表来实现它,但我得到了所有颜色的汽车。我只需要实际颜色(最后添加)。
请帮忙
尝试这个:
select c.id, colorname
from cars c
inner join CarColorHistory h on c.id = h.CarID
inner join Color c2 on h.colorid = c2.id
where h.ModificationDate = (select max(ModificationDate)
from CarColorHistory x where c.id = x.CarId)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)