我有这样的数据:
header_id | class | start_date | end_date
-------------------------------------------
1 | c1 | 20-08-2019 | 22-08-2019
1 | c1 | 22-08-2019 | 24-08-2019
1 | c2 | 24-08-2019 | 27-08-2019
2 | c3 | 21-08-2019 | 22-08-2019
2 | c2 | 22-08-2019 | 25-08-2019
2 | c3 | 25-08-2019 | 26-08-2019
我想要得到以下结果:
header_id | class | start_date | end_date
-------------------------------------------
1 | c1 | 20-08-2019 | 24-08-2019
1 | c2 | 24-08-2019 | 27-08-2019
2 | c3 | 21-08-2019 | 22-08-2019
2 | c2 | 22-08-2019 | 25-08-2019
2 | c3 | 25-08-2019 | 26-08-2019
如果 header_id 相同,class 相同,且前一条数据的结束日期与后一条数据的开始日期相同,则取开始日期中的最小日期和结束日期中的最大日期。但是,如果前一条数据的结束日期与后一条数据的开始日期不同,则数据将正常显示。
有办法得到这个结果吗?
最佳答案
SELECT header_id, class, MIN(start_date), MAX(end_date) FROM your-table
GROUP BY header_id, class
关于mysql - 将mysql中同一个表的行值与条件进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59640725/