drvpv7995 2012-03-30 15:14 采纳率: 100%
浏览 32
已采纳

获取范围从天

I have this table structure:

EDIT more complex example: add hidden range

category|   day      |   a   |
--------|------------|-------|
1       | 2012-01-01 |   4   |
1       | 2012-01-02 |   4   |
1       | 2012-01-03 |   4   |
1       | 2012-01-04 |   4   |
1       | 2012-01-05 |   5   |
1       | 2012-01-06 |   5   |
1       | 2012-01-07 |   5   |
1       | 2012-01-08 |   4   |
1       | 2012-01-09 |   4   |
1       | 2012-01-10 |   4   |
1       | 2012-01-11 |   5   |
1       | 2012-01-12 |   5   |
1       | 2012-01-16 |   5   |
1       | 2012-01-17 |   5   |
1       | 2012-01-18 |   5   |
1       | 2012-01-19 |   5   |
...

with 'category-day' as unique keys. I would extract a range of dates, for each category, according with column "a" and given limit range, like so:

1,2012-01-01|2012-01-04,4
1,2012-01-05|2012-01-07,5
1,2012-01-08|2012-01-10,4
1,2012-01-11|2012-01-12,5
1,2012-01-13|2012-01-15,0
1,2012-01-16|2012-01-19,5

or similar.

I search the best way for do it. Using only mysql preferably but also with a little bit of php.

NOTE1: not all day are inserted: between two days non-contiguos could not be other days. In this case I would in output the missed range with column "a" = 0.

NOTE2: I did it with a simple query and some rows of php but I don't like it because my simple algorithm need a cycle for each day in range multiplied for each category found. If range is too big and there are too much categories, that's not so good.

FINAL EDIT: OK! After reading all comments and answers, I think not exists a valid, efficient and, at same time, readable solution. So Mosty Mostacho answer is a no 100% valid solution, but it has 100% valid suggestions. Thank you all.

  • 写回答

8条回答 默认 最新

  • dqgo99177 2012-04-06 18:58
    关注

    New edit:

    As I told you in a comment, I strongly recommend you to use the quick query and then process the missing dates in PHP as that would be faster and more readable:

    select
      concat(@category := category, ',', min(day)) col1,
      concat(max(day), ',', @a := a) col2
    from t, (select @category := '', @a := '', @counter := 0) init
    where @counter := @counter + (category != @category or a != @a)
    group by @counter, category, a
    

    However, if you still want to use the query version, then try this:

    select
      @counter := @counter + (category != @category or a != @a) counter,
      concat(@category := category, ',', min(day)) col1,
      concat(max(day), ',', @a := a) col2
    from (
      select distinct s.day, s.category, coalesce(t1.a, 0) a
      from (
        select (select min(day) from t) + interval val - 1 day day, c.category
        from seq s, (select distinct category from t) c
        having day <= (select max(day) from t)
      ) s
      left join t t1 on s.day = t1.day and s.category = t1.category
      where s.day between (
        select min(day) from t t2
        where s.category = t2.category) and (
        select max(day) from t t2
        where s.category = t2.category)
      order by s.category, s.day
    ) t, (select @category := '', @a := '', @counter := 0) init
    group by counter, category, a
    order by category, min(day)
    

    Note that MySQL won't allow you to create data on the fly, unless you hardcode UNIONS, for example. This is an expensive process that's why I strongly suggest you to create a table with only an integer field with values from 1 to X, where X is, at least the maximum amount of dates that separate the min(day) and max(day) from your table. If you're not sure about that date, just add 100,000 numbers and you'll be able to generate range periods for over 200 years. In the previous query, this table is seq and the column it has is val.

    This results in:

    +--------------+--------------+
    |     COL1     |     COL2     |
    +--------------+--------------+
    | 1,2012-01-01 | 2012-01-04,4 |
    | 1,2012-01-05 | 2012-01-07,5 |
    | 1,2012-01-08 | 2012-01-10,4 |
    | 1,2012-01-11 | 2012-01-12,5 |
    | 1,2012-01-13 | 2012-01-15,0 |
    | 1,2012-01-16 | 2012-01-19,5 |
    +--------------+--------------+
    

    Ok, I'm lying. The result is actually returning a counter column. Just disregard it, as removing it (using a derived table) would be even less performant!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源