请问如何找到被sql中断的练习?

时间:2019-01-22 06:58:34

标签: mysql

我有一个像这样的表

person_id, pe_year, exercise_keep_year
1001, 2016, 12
1001, 2018, 14
1002, 2017, 5
1002, 2018, 0
1003, 2016, 10
1003, 2017, 0
1003, 2018, 1
1003, 2019, 2

如果该人每年继续锻炼,则pe_year-exercise_keep_year都将相等。(例如person_id = 1001,exercise_start_year = 2016-12 = 2018-14 = 2004)

如果运动被中断,pe_year-exercise_keep_year将不完全相等。 (例如person_id = 1003)

我想找到所有被中断的person_id。请怎么做?

2 个答案:

答案 0 :(得分:1)

尝试一下

select person_id from
(select person_id,count(distinct(pe_year-exercise_keep_year)) as cnt from tablename group by person_id) as a
where cnt>1

答案 1 :(得分:1)

select person_id from t
group by person_id
having count( distinct pe_year - exercise_keep_year ) <> 1