所以你需要什麼是二INNER JOIN
s對VehicleSpecs
表,每個Car_Id1
和Car_Id2
一個。我把它們別名爲car1, car2
。
SELECT TOP 100
c.Id,
c.Slug,
c.TimeStamp,
/* Select the relevant columns from *both* of the joined tables */
/* And give each column an alias to differentiate it from the other */
car1.Year AS car1Year,
car1.Make AS car1Make,
car1.Model AS car1Model,
car2.Year AS car2Year,
car2.Make AS car2Make,
car2.Model AS car2Model
FROM
Comparisons c
/* Join first against VehicleSpecs for Car_Id1 */
INNER JOIN VehicleSpecs car1 ON c.Car_Id1 = car1.Id
/* Then once more for Car_Id2 */
INNER JOIN VehicleSpecs car2 ON c.Car_Id2 = car2.Id
ORDER BY c.TimeStamp
你說你想要的最新,所以我想你實際上意味着對時間戳使用降序排列:
ORDER BY c.TimeStamp DESC
謝謝!有用。 –