为什么我在 WHERE 子句中使用硬编码值时在 Group BY 语句中出现错误?
Why am I getting Error in Group BY statement while using hardcoded values in WHERE clause?
我是 PostgreSQL 的新手。我正在开发出租车预订应用程序。
我正在尝试找出 (1) 不起作用的原因。硬编码值将由用户输入替换。
不工作:
SELECT MAX(cr.pickup_point) AS pickup_point , MAX(cr.destination) AS 目的地,
MAX(cr.leave_time) AS leave_time,MAX(cr.license) AS 许可证,
MAX(cr.username) AS 用户名, MAX(cr.car_ride_id) AS car_ride_id ,
最大(isd.no_of_seats)作为no_of_seats
从 car_ride cr , is_driver isd WHERE cr.username = isd.username
AND cr.license = isd.license AND
低(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)> 现在()::时间戳(0)
分组依据(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 且 b.successful 为真
AND b.leave_time >now()::timestamp(0) AND cr2.leave_time = b.leave_time
AND cr.car_ride_id = cr2.car_ride_id)
2.Working:
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) 显示所有未来未完全预订的乘车广告 (出价数量 = TRUE 对于特定 car_ride_id < car_capacity)因为车还没有订满
请注意,我已多次使用 MAX 聚合,因为我已将它们用作后续表单中的隐藏字段以进行出价。
Results for 2nd query
Error message upon running the 1st query.
错误:"GROUP"
处或附近的语法错误
第 10 行:分组依据(cr.car_ride_id,isd.no_of_seats)
在 AND
和 LOWER
之间,您有一个没有关闭的不必要的括号:
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)
以更具可读性的方式格式化查询时,更容易避免这些错误。
我是 PostgreSQL 的新手。我正在开发出租车预订应用程序。
我正在尝试找出 (1) 不起作用的原因。硬编码值将由用户输入替换。
不工作:
SELECT MAX(cr.pickup_point) AS pickup_point , MAX(cr.destination) AS 目的地, MAX(cr.leave_time) AS leave_time,MAX(cr.license) AS 许可证, MAX(cr.username) AS 用户名, MAX(cr.car_ride_id) AS car_ride_id , 最大(isd.no_of_seats)作为no_of_seats 从 car_ride cr , is_driver isd WHERE cr.username = isd.username AND cr.license = isd.license AND 低(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)> 现在()::时间戳(0) 分组依据(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 且 b.successful 为真 AND b.leave_time >now()::timestamp(0) AND cr2.leave_time = b.leave_time AND cr.car_ride_id = cr2.car_ride_id)
2.Working:
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) 显示所有未来未完全预订的乘车广告 (出价数量 = TRUE 对于特定 car_ride_id < car_capacity)因为车还没有订满
请注意,我已多次使用 MAX 聚合,因为我已将它们用作后续表单中的隐藏字段以进行出价。
Results for 2nd query
Error message upon running the 1st query.
错误:"GROUP"
处或附近的语法错误第 10 行:分组依据(cr.car_ride_id,isd.no_of_seats)
在 AND
和 LOWER
之间,您有一个没有关闭的不必要的括号:
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)
以更具可读性的方式格式化查询时,更容易避免这些错误。