我需要从选择最大值中获取数据

时间:2018-12-24 12:23:57

标签: mysql

我正在处理我正在处理的记录,但是我的MySQL存在问题,我无法在特定日期获得select的最大值
我有此声明可获取价值:

select re.date, an.userAnswers , count(an.userAnswers) as countQues 
from reviwes re,answers an 
where an.userReviewsId=re.id 
and an.questionType!=5 
and re.campainId=23 
and CAST(re.date AS DATE) >= "2018-11-01 00:00:00" 
and CAST(re.date AS DATE) <= "2018-11-07 23:59:59" 
group by DATE_FORMAT(re.date, "%d-%m-%y"),an.userAnswers


+---------------------+-------------+-----------+
| date                | userAnswers | countQues |
+---------------------+-------------+-----------+
| 2018-11-01 21:37:00 | 1           |         5 |
| 2018-11-01 11:53:00 | 10          |        58 |
| 2018-11-01 11:53:00 | 2           |       154 |
| 2018-11-01 14:16:00 | 3           |        79 |
| 2018-11-01 14:16:00 | 4           |        25 |
| 2018-11-01 11:53:00 | 5           |       498 |
| 2018-11-01 23:29:00 | 7           |         3 |
| 2018-11-01 20:37:00 | 8           |         5 |
| 2018-11-01 18:01:00 | 9           |         9 |
| 2018-11-02 14:06:00 | 1           |        10 |
| 2018-11-02 10:36:00 | 10          |        70 |
| 2018-11-02 10:36:00 | 2           |       193 |
| 2018-11-02 10:36:00 | 3           |       125 |
| 2018-11-02 18:13:00 | 4           |        38 |
| 2018-11-02 10:36:00 | 5           |       585 |
etc.. 

现在我需要使用同一条语句来最大化价值,我尝试这样做:

select max(cast(userAnswersGet.userAnswers as int))
  ,userAnswersGet.userAnswers
  ,userAnswersGet.date
  ,userAnswersGet.countQues 
from (
    select re.date, an.userAnswers , count(an.userAnswers) as countQues 
    from reviwes re,answers an 
    where an.userReviewsId=re.id 
    and an.questionType!=5 
    and re.campainId=23 
    and CAST(re.date AS DATE) >= "2018-11-01 00:00:00" 
    and CAST(re.date AS DATE) <= "2018-11-07 23:59:59" 
    group by DATE_FORMAT(re.date, "%d-%m-%y"),an.userAnswers
) as userAnswersGet 
group by DATE_FORMAT(userAnswersGet.date, "%d-%m-%y") 
having max(cast(userAnswersGet.userAnswers as int));

我希望得到这个输出:

                +---------------------+-------------+-----------+
                | date                | userAnswers | countQues |
                +---------------------+-------------+-----------+
                | 2018-11-01 11:53:00 | 10          |        58 |
                | 2018-11-02 14:06:00 | 10          |        70 |

1 个答案:

答案 0 :(得分:-1)

1-快速提醒: 使用分组依据时,如果不是聚合函数,则必须将select中的所有列都放入。 示例:

SELECT A, Max(A) FROM MyTable GROUP BY A

2-此查询是正确的,但我不确定您要做什么:

select max(userAnswersGet.userAnswers) as 'userAnswers'      
  ,userAnswersGet.date
  ,userAnswersGet.countQues 
from (
    select re.date, an.userAnswers , count(an.userAnswers) as countQues 
    from reviwes re,answers an 
    where an.userReviewsId=re.id 
    and an.questionType!=5 
    and re.campainId=23 
    and CAST(re.date AS DATE) >= "2018-11-01 00:00:00" 
    and CAST(re.date AS DATE) <= "2018-11-07 23:59:59" 
    group by DATE_FORMAT(re.date, "%d-%m-%y"),an.userAnswers
) as userAnswersGet 
group by DATE_FORMAT(userAnswersGet.date, "%d-%m-%y"),  userAnswersGet.countQues;