简体   繁体   English

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

[英]i need to get data from select max value

i'm working on records that i have working on it but there is problem with my mysql is i can't get the max value of select with specific date我正在处理我正在处理的记录,但我的 mysql 有问题是我无法获得具有特定日期的 select 的最大值
i have this statement to get value :我有这个声明来获得价值:

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.. 

now i need to max value with the same statement, i try this :现在我需要用相同的语句获得最大值,我试试这个:

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));

and i expected to get output this:我希望得到这样的输出:

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

1 - Quick reminder: When you use a Group by, you must put all the columns from the select if it's not an aggregative function. 1 - 快速提醒:当您使用 Group by 时,如果它不是聚合函数,则必须放置 select 中的所有列。 Example :例子 :

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

2- This query is correct but I am not sure of what you want to do : 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM