尝试从此查询中一次选择一行(例如,rank = 1)。但是,它并不起作用,因为"未知列'排名'。如果我把它改成" WHERE id = 1"然后它起作用了,但由于某种原因它甚至不知道它的等级是什么等级
SET @rownum=0;
SELECT @rownum := @rownum + 1 AS rank, id, client, date, time, pickupCity, pickupState
FROM (
SELECT r.id, CONCAT(c.fname, ' ', c.lname) as client, r.date,
LOWER(TIME_FORMAT(r.time, '%l:%i%p')) as time, r.pickupCity, r.pickupState
FROM request r
INNER JOIN client c ON r.client = c.id
INNER JOIN pickup p ON r.id = p.request
INNER JOIN driver d ON d.id = p.driver
WHERE date = '2018-04-18' AND d.id = 1
GROUP BY time
) AS tab
HAVING rank = 1;
答案 0 :(得分:2)
在MySQL中,您可以使用try
:
HAVING
注意:
SELECT @rownum := @rownum + 1 AS rank, id, client, date, time, pickupCity, pickupState
FROM (SELECT r.id, CONCAT(c.fname, ' ', c.lname) as client, r.date,
LOWER(TIME_FORMAT(r.time, '%l:%i%p')) as time, r.pickupCity,
r.pickupState
FROM request r JOIN
client c
ON r.client = c.id JOIN
driver d
ON ?
pickup p
ON d.id = p.driver
WHERE date = '2018-04-18' AND d.id = 1
GROUP BY time
) t CROSS JOIN
(SELECT @rank := 0) params
HAVING rank = 1;
适用于缺失的??
条件。JOIN
语法的工作。您应该学习正确的,明确的,标准的JOIN
语法。JOIN
;你不需要另外发表声明。@rank
毫无意义,因为GROUP BY
中有许多未分段的列。如果我不得不推测,问题的根本原因是缺少SELECT
条件,并且您已经付出了很多努力来解决这个问题。