在WHERE子句中使用硬编码值时,为什么在Group BY语句中出现错误?

时间:2018-11-06 09:28:23

标签: php database postgresql group-by phppgadmin

我是PostgreSQL的新手。我正在开发出租车预订应用程序。

我试图找出(1)无效的原因。硬编码值将由用户输入代替。

  1. 不工作:

    选择MAX(cr.pickup_point)AS拾取点,MAX(cr.destination)AS目标, MAX(cr.leave_time)AS离开时间,MAX(cr.license)AS许可, MAX(cr.username)AS用户名,MAX(cr.car_ride_id)AS car_ride_id, MAX(isd.no_of_seats)AS no_of_seats 从car_ride cr,is_driver isd在哪里cr.username = isd.username AND cr.license = isd.license AND LOWER(TRIM(pickup_point)):: varchar = LOWER(TRIM('Paya Lebar East,Paya Lebar')):: varchar AND(LOWER(TRIM(cr.destination)):: varchar = LOWER(TRIM('Kranji,Sungei Kadut')):: varchar AND(cr.leave_time)> now():: timestamp(0) GROUP BY(cr.car_ride_id,isd.no_of_seats) 拥有isd.no_of_seats> ALL(从出价b中选择COUNT(*)条,car_ride cr2在哪里 b.driver_username = cr2.username AND b.successful是TRUE AND b.leave_time> now():: timestamp(0)AND cr2.leave_time = b.leave_time AND cr.car_ride_id = cr2.car_ride_id)

2。工作:

SELECT  MAX(cr.pickup_point) AS pickup_point , MAX(cr.destination)
AS destination , MAX(cr.leave_time) AS leave_time ,
MAX(cr.license) AS license , MAX(cr.username)
AS username, MAX(cr.car_ride_id) AS car_ride_id , MAX(isd.no_of_seats) AS no_of_seats
FROM car_ride  cr , is_driver isd WHERE cr.username = isd.username
AND cr.license =  isd.license AND
(cr.leave_time)> now()::timestamp(0) GROUP BY(cr.car_ride_id, isd.no_of_seats)  HAVING
isd.no_of_seats>
ALL (SELECT COUNT(*) FROM bids b , car_ride cr2 WHERE
b.driver_username = cr2.username AND b.successful IS TRUE
AND  b.leave_time >now()::timestamp(0) AND cr2.leave_time = b.leave_time
AND cr.car_ride_id = cr2.car_ride_id )

(1。)和(2.)之间的区别在于,前者打算根据接送地点和目的地来过滤结果。

虽然(2)显示了将来所有尚未预订的所有乘车广告(对于特定的car_ride_id

请注意,我已经多次使用MAX汇总,因为我将它们用作后续出价形式的隐藏字段。

Results for 2nd query

Error message upon running the 1st query.

错误:“组”或附近的语法错误

第10行:GROUP BY(cr.car_ride_id,isd.no_of_seats)

1 个答案:

答案 0 :(得分:0)

ANDLOWER之间,您有一个没有关闭的不必要的括号:

  AND (LOWER(TRIM(cr.destination))::varchar = LOWER(TRIM('Kranji, Sungei Kadut'))::varchar

已修复:

SELECT MAX(cr.pickup_point) AS pickup_point , MAX(cr.destination) AS destination,
       MAX(cr.leave_time) AS leave_time,MAX(cr.license) AS license ,
       MAX(cr.username) AS username, MAX(cr.car_ride_id) AS car_ride_id ,
       MAX(isd.no_of_seats) AS no_of_seats
  FROM car_ride cr , is_driver isd
 WHERE cr.username = isd.username
   AND cr.license = isd.license
   AND LOWER(TRIM(pickup_point))::varchar = LOWER(TRIM('Paya Lebar East, Paya Lebar'))::varchar
   AND /*(*/LOWER(TRIM(cr.destination))::varchar = LOWER(TRIM('Kranji, Sungei Kadut'))::varchar
   AND (cr.leave_time)> now()::timestamp(0)
 GROUP BY(cr.car_ride_id, isd.no_of_seats)
HAVING isd.no_of_seats> ALL (SELECT COUNT(*)
                               FROM bids b , car_ride cr2
                              WHERE b.driver_username = cr2.username
                                AND b.successful IS TRUE
                                AND b.leave_time >now()::timestamp(0)
                                AND cr2.leave_time = b.leave_time
                                AND cr.car_ride_id = cr2.car_ride_id)

以更具可读性的格式设置查询格式时,更容易避免这些错误。