【问题标题】:Compare rows values of same table in mysql比较mysql中同一张表的行值
【发布时间】:2020-01-08 04:05:49
【问题描述】:

我有这样的数据:

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

我想得到以下结果:

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

有没有办法得到这个结果?

【问题讨论】:

  • 使用2019-12-31格式的日期,否则比较、取最小值/最大值等将失败。

标签: mysql sql compare mariadb


【解决方案1】:

按照我的理解,你需要这样的东西:

select header_id. class, min(start_date), max(end_date)
from your_table_name
group by header_id, class

your_table_name更改为实际名称

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多