选择goup后的第一行

时间:2017-06-07 09:57:55

标签: mysql

我有下面的查询,它返回一个小于20的类的名称,如果没有小于20的类,则添加一个新的类。问题是,当我从类中删除一个用户时,该查询返回两行。 我只需要获得返回的第一行。

SELECT exam_venue AS venue 
  FROM application 
 WHERE class_level = 1 
   AND rownumber = 1
 GROUP 
    BY venue 
HAVING COUNT(id) < 20

2 个答案:

答案 0 :(得分:0)

SELECT exam_venue AS场所FROM应用程序WHERE class_level = 1 AND rownumber = 1 GROUP BY场地HAVING COUNT(id)&lt; 20 LIMIT 0,1

答案 1 :(得分:-1)

一种简单的方法可以是以下:::

SELECT TOP 1 exam_venue AS venue, COUNT(id)
FROM application 
WHERE class_level = 1 
GROUP BY venue 
HAVING COUNT(id) < 20