MySQL Join使用WHERE子句返回太多行

时间:2019-05-09 12:31:48

标签: mysql join

我在使用SQL查询时遇到麻烦。因此,在我的项目中,用户可以保留行程。我想按用户ID(passenger_id显示保留的游乐设施,但是当用户仅为其中一个驾驶员广告保留游乐设施时,查询返回所有用户(driver_id)的广告。

    SELECT advertisement.id
         , COUNT(review.driver_id) AS 'review_count'
         , ROUND(AVG(review.mark) ,1) AS 'rating'
         , users.unique_id
         , users.name
         , users.surname
         , users.phone
         , YEAR(CURDATE()) - YEAR(users.birthdate) AS age
         , users.image
         , advertisement.from_city
         , advertisement.to_city
         , users.car_name
         , users.car_model
         , users.car_make_year
         , advertisement.number_of_places
         , advertisement.price
         , advertisement.datetime
         , advertisement.info
      FROM reserved_rides
      JOIN advertisement  
        ON reserved_rides.driver_id = advertisement.user_id
      LEFT 
      JOIN review 
        ON reserved_rides.driver_id = review.driver_id
      JOIN users 
        ON reserved_rides.driver_id = users.unique_id
     WHERE reserved_rides.passenger_id = ?
     GROUP 
        BY advertisement.id
     ORDER 
        BY advertisement.datetime ASC

这是怎么回事?

1 个答案:

答案 0 :(得分:0)

我希望将GROUP BY advertisement.id替换为GROUP BY reserved_rides.driver_id可以解决您的问题。欢呼声