MySQL-获取重复的连续行

时间:2018-11-15 17:49:42

标签: mysql

我试图弄清楚如何为表中的每个id_user重复获取连续的id_question。假设我有这个:

+-----+---------------+---------+--------+
| id  |  id_question  | id_user | value  |
+-----+---------------+---------+--------+
| 1   | 1             |   1     |   1000 |
| 2   | 1             |   1     |   2000 |
| 3   | 2             |   2     |   2200 |
| 4   | 2             |   2     |   1100 |
| 5   | 3             |   1     |   3000 |
+---------------------+---------+--------+

我想获取具有相同连续问题(id_user)的用户(id_question

因此,在这种情况下,输出为:

+-------------+---------+--------+
| id_question | id_user | count  |
+-------------+---------+--------+
| 1           |   1     |   2    |
| 2           |   2     |   2    |
+-------------+---------+--------+

因为id_user 1必须连续id_question等于等于id_user 2的值。

1 个答案:

答案 0 :(得分:0)

select id_question, id_user, count(*) as Amount
from yourTable
group by id_question, id_user
having amount > 1