SQL组之间

时间:2017-07-22 04:31:46

标签: mysql sql

我有这个SQL代码

SELECT user_id, COUNT(*) 
FROM crate_log 
GROUP BY user_id, last_crate - first_crate 
HAVING COUNT(*) > 1 
ORDER BY COUNT(*) DESC

我想要做的是从" last_crate - first_crate"获得所有结果。向上5和向下5并计算它们。

喜欢:

last_crate - first_crate BETWEEN last_crate - first_crate - 5 
                             AND last_crate - first_crate + 5

我尝试了一些事情,但不知道该怎么做。

顺便说一句。

last_crate - first_crate

只是我需要的差异

希望你理解我..

1 个答案:

答案 0 :(得分:0)

怎么样

SELECT user_id, last_crate - first_crate, COUNT(*) 
FROM crate_log 
GROUP BY user_id, last_crate - first_crate 
HAVING COUNT(*) > 1 AND ABS(last_crate - first_crate)<=5
ORDER BY COUNT(*) DESC