我有两张桌子:
tbl_car_detail
--------------
CarID (primary key)
CarBrand
和
tbl_car_image
-------------
ImageID (primary key)
CarID (foreign key)
Image
我已将以下记录插入tbl_car_detail
:
CarID CarBrand
-----------------------------
1 Audi
2 bwm
我正在使用相同的CarID
将多张图片插入tbl_car_Image
,如下所示:
ImageID CarID Image
------------------------------------------------
1 1 Image1
2 1 Image2
3 1 Image3
4 2 Image4
5 2 Image5
我想在两个表中的一个结果集中显示结果。我正在使用此查询:
SELECT dbo.Tbl_Car_detail.CarID,dbo.tbl_mst_CarDetail.CarBrand , tbl_car_Image.Image
FROM dbo.Tbl_Car_detail.CarID RIGHT OUTER JOIN
tbl_car_Image.Image ON dbo.Tbl_Car_detail.CarID = dbo.tbl_car_Image.CarID
结果如下:
CarID CarBrand Image
--------------------------------------------------
1 Audi Image1
1 Audi Image2
1 Audi Image3
2 Bmw Image4
2 Bmw Image5
但是,我只希望每个CarID结果只有一个图像
CarID CarBrand Image
--------------------------------------------------
1 Audi Image1
2 Bmw Image4
答案 0 :(得分:0)
所以你只想要每个CarID的第一张图片?这样做......
SELECT dbo.Tbl_Car_detail.CarID,dbo.tbl_mst_CarDetail.CarBrand,(SELECT TOP 1 tbl_car_Image.Image FROM tbl_car_Image.Image WHERE dbo.Tbl_Car_detail.CarID=dbo.tbl_car_Image.CarID) AS Image FROM dbo.Tbl_Car_detail.CarID