4

假设我有 3 张桌子:

  1. 汽车

    • ID
  2. 汽车颜色历史

    • ID
    • 车牌
    • 颜色ID
    • 修改日期
  3. 颜色

    • ID
    • 颜色名称

我想选择所有汽车及其颜色,但重要的是,汽车的颜色是CarColorHistory表中最后修改的颜色。

我需要使用 join 来做到这一点。

例子:

汽车:

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

颜色:

1 Blue
2 Red

我需要得到结果:(car id, colorName)

1 Blue
2 Red

我尝试通过加入 Cars 表和 CarColorHistory 表来实现它,但我得到了所有颜色的汽车。我只需要实际颜色(最后添加)。

请帮忙

4

6 回答 6

4

尝试这个:

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)
于 2013-03-26T14:12:33.623 回答
3

有几种方法可以获得结果。您可以使用子查询来获取max(modificationdate)

select c.id, r.colorname
from cars c
inner join CarColorhistory h1
  on c.id = h1.carid
inner join
(
  select max(modificationdate) MaxDate,
    carid
  from CarColorhistory
  group by carid
) h2
  on h1.carid = h2.carid
  and h1.modificationdate = h2.maxdate
inner join color r
  on h1.colorid = r.id

请参阅带有演示的 SQL Fiddle

或者由于您使用的是 SQL Server,您可以使用排名函数

select id, colorname
from
(
  select c.id, r.colorname,
    row_number() over(partition by c.id order by modificationdate desc) rn
  from cars c
  inner join CarColorhistory h1
    on c.id = h1.carid
  inner join color r
    on h1.colorid = r.id
) src
where rn = 1;

请参阅带有演示的 SQL Fiddle

于 2013-03-26T14:14:16.667 回答
1

这应该为您解决问题:

SELECT c.id, (
    SELECT co.ColorName FROM Color co
    WHERE co.id = (
        SELECT TOP 1 ColorID FROM CarColorHistory
        WHERE CarID = c.id
        ORDER BY ModificationDate DESC
    )
 ) AS ColorName
于 2013-03-26T14:10:25.730 回答
0

如果你有Sql Server 2005或更大,你可以试试这个:

你可以在这里试试,效果如何Common table expressionSql Fiddle demo

  ;WITH CTE_Cars(CarID, MaxDate)
    AS
    (
    SELECT CarID, MAX(ModificataionDate) AS MaxDate
    FROM CarColorHistory
    GROUP BY CarID
    )
    SELECT CTE_Cars.CarID, Color.ColorName
    FROM 
    CTE_Cars
    INNER JOIN CarColorHistory ON CarColorHistory.CarID = CTE_Cars.CarID AND
    CarColorHistory.ModificataionDate = CTE_Cars.MaxDate
    INNER JOIN Color ON Color.id = CarColorHistory.ColorId
于 2013-03-26T14:22:42.447 回答
0

一种方法可能是只使用子查询,就像之前发布的那样,因为您使用的是 t-sql,您也应该能够通过应用来做到这一点:

SELECT
        Cars.Id, LatestColors.ColorID, LatestColors.ModificationDate
    FROM Cars
    CROSS APPLY (
        SELECT TOP 1
                ColorID, ModificationDate
            FROM CarColorHistory
            WHERE CarID = Cars.ID
            ORDER BY ModificationDate DESC
    ) AS LatestColors
于 2013-03-26T14:18:59.713 回答
0

我不确定这是否是最好的方法,但我就是这样做的。首先使用 MAX 从表格中获取您想要的值,然后将该结果用作表格JOIN以消除不需要的值。

SELECT c.ID, Color.Color
From Cars c JOIN CarColorHistory h on c.id = h.CarID
     JOIN Color on h.ColorID = Color.ID
     JOIN

    --Create a table with only the latest value and the car ID
    ( 
    SELECT c.ID, Max(h.TimeStamp) as time
    FROM Cars c JOIN CarColorHistory h on c.id = h.CarID
         JOIN Color on h.ColorID = Color.ID
    Group by c.ID --Join the table on latest time to get rid of old timestamps
    ) Max on Max.ID = c.ID and h.TimeStamp=max.time
于 2013-03-26T14:30:32.797 回答