仅包括查询结果中的第一个组

时间:2015-07-13 21:31:02

标签: mysql sql

给定架构

enter image description here

以下查询

SELECT a.user_id,
  a.date_created,
  avg(ai.level) level
FROM assessment a
  JOIN assessment_item ai ON a.id = ai.assessment_id
GROUP BY a.user_id, a.date_created;

返回这些结果

user_id, a.date_created,        level
1,       "2015-07-13 18:26:00", 4.0000  
1,       "2015-07-13 19:04:58", 6.0000  
13,      "2015-07-13 18:26:00", 2.0000  
13,      "2015-07-13 19:04:58", 3.0000  

我想更改查询,以便只为每个用户返回最早的结果。换句话说,应该返回以下内容

user_id, a.date_created,        level
1,       "2015-07-13 18:26:00", 4.0000
13,      "2015-07-13 18:26:00", 2.0000

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案:使用用户变量来索引'行,然后过滤第一行:

ERROR_FILE_NOT_FOUND