比较mysql中相同表的行值
我有这样的数据:比较mysql中相同表的行值,mysql,sql,compare,mariadb,Mysql,Sql,Compare,Mariadb,我有这样的数据: 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 | 25-
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 | 25-08-2019 | 26-08-2019
2 | c3 | 26-08-2019 | 30-08-2019
3 | c1 | 23-08-2019 | 26-08-2019
select header_id. class, min(start_date), max(end_date)
from your_table_name
group by header_id, class
我希望得到以下结果:
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 | 25-08-2019 | 30-08-2019
3 | c1 | 23-08-2019 | 26-08-2019
有没有办法得到这个结果?根据我的理解,你需要这样的东西:
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 | 25-08-2019 | 26-08-2019
2 | c3 | 26-08-2019 | 30-08-2019
3 | c1 | 23-08-2019 | 26-08-2019
select header_id. class, min(start_date), max(end_date)
from your_table_name
group by header_id, class
将
您的\u表\u名称
更改为实际名称根据我的理解,您需要这样的名称:
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 | 25-08-2019 | 26-08-2019
2 | c3 | 26-08-2019 | 30-08-2019
3 | c1 | 23-08-2019 | 26-08-2019
select header_id. class, min(start_date), max(end_date)
from your_table_name
group by header_id, class
将
您的\u表\u名称
更改为实际名称使用格式为2019-12-31
的日期,否则比较、取最小值/最大值等将失败。使用格式为2019-12-31
的日期,否则比较、取最小值/最大值等将失败。