在select语句中的Select语句中找不到列

时间:2018-04-05 14:24:00

标签: mysql sql

尝试从此查询中一次选择一行(例如,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;

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条件,并且您已经付出了很多努力来解决这个问题。