phpmyadmin 在PHP和MySQL中选择带有图像的汽车列表

mfpqipee  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(118)

我有一个查询,它从MySQL数据库中选择一个CAR和它的IMAGES

SELECT car.*, comments.*, person.*, body_type.* FROM car
INNER JOIN person
ON person.car_id = car.car_id 
INNER JOIN comments
ON comments.car_id = car.car_id 
INNER JOIN body_type
ON body_type.bodytype_id = car.bodytype_id
INNER JOIN images
ON images.car_id = car.car_id
ORDER BY added_on DESC

然而,这实际上是对来自IMAGES的图像进行计数,并返回一个列表,次数与来自表的图像的次数相同。例如,如果来自IMAGES表的图像有五个,则它将返回一个CAR列表五次。我注意到问题出在查询中,因为当我删除INNER JOIN images ON images.car_id = car.car_id时,汽车以正确的方式显示,除了没有图像。

agxfikkp

agxfikkp1#

你想得到每辆车的图片数量吗?也许这会帮助你(未经测试):

SELECT car.*, comments.*, person.*, body_type.*, count(*) as countImages
FROM car
INNER JOIN person
ON person.car_id = car.car_id 
INNER JOIN comments
ON comments.car_id = car.car_id 
INNER JOIN body_type
ON body_type.bodytype_id = car.bodytype_id
INNER JOIN images
ON images.car_id = car.car_id
GROUP BY car.car_id
ORDER BY added_on DESC

相关问题