选择列值更改时的日期

时间:2013-05-06 13:48:48

标签: php mysql

我有一张这样的表

| ID | CAMPAIGN_ID | CHARGES |                            DATE |
----------------------------------------------------------------
|  1 |           1 |      15 | November, 17 2012 09:53:08+0000 |
|  2 |           1 |      15 | November, 18 2012 09:53:08+0000 |
|  3 |           1 |      15 | November, 22 2012 09:53:08+0000 |
|  4 |           1 |      15 | November, 24 2012 09:53:08+0000 |
|  5 |           1 |      15 | November, 26 2012 09:53:08+0000 |
|  6 |           1 |      10 | November, 27 2012 09:53:08+0000 |
|  7 |           1 |      10 | November, 28 2012 09:53:08+0000 |
|  8 |           1 |      10 | December, 02 2012 09:53:08+0000 |
|  9 |           1 |      10 | December, 04 2012 09:53:08+0000 |
| 10 |           1 |      10 | December, 05 2012 09:53:08+0000 |
| 11 |           1 |      10 | December, 06 2012 09:53:08+0000 |
| 12 |           1 |      10 | December, 07 2012 09:53:08+0000 |
| 13 |           1 |      15 | December, 08 2012 09:53:08+0000 |
| 14 |           1 |      15 | December, 09 2012 09:53:08+0000 |
| 15 |           1 |      15 | December, 10 2012 09:53:08+0000 |
| 16 |           1 |      15 | December, 12 2012 09:53:08+0000 |
| 17 |           1 |      15 | December, 13 2012 09:53:08+0000 |

SQL Fiddle Schema

我想选择收费正在变化的日期。输出在这里

| ID | CAMPAIGN_ID | CHARGES |                            DATE |
----------------------------------------------------------------
|  6 |           1 |      10 | November, 27 2012 09:53:08+0000 |
| 13 |           1 |      15 | December, 08 2012 09:53:08+0000 |

我如何用mysql实现这一目标。

2 个答案:

答案 0 :(得分:1)

MySQL没有窗口函数,但您可以使用变量执行此操作。例如。类似的东西(注意:由于重复别名,它可能是无效的sql,但它应该让你走上正轨):

select ranked_campaigns.id, ranked_campaigns.campaign_id, ranked_campaigns.charges, ranked_campaigns.date
from (
  select id, campaign_id, charges, date, @rank = @rank + 1 as rank
  from ( select @rank := 1) as r,
       ( select id, campaign_id, charges, date
         from campaigns
         order by campaign_id, date ) as raw_campaigns
  ) as ranked_campaigns
join (
  select id, campaign_id, charges, date, @rank = @rank + 1 as rank
  from ( select @rank := 1) as r,
       ( select id, campaign_id, charges, date
         from campaigns
         order by campaign_id, date ) as raw_campaigns
  ) as ranked_campaigns2
on ranked_campaigns.rank = ranked_campaigns2.rank - 1
and ranked_campaigns.campaign_id = ranked_campaigns2.campaign_id
and ranked_campaigns.charges <> ranked_campaigns2.charges;

答案 1 :(得分:1)

这是另一种方式......

SELECT b.*
  FROM
     ( SELECT x.* 
     , COUNT(*) rank
  FROM campaign_charges x
  JOIN campaign_charges y
    ON y.campaign_id = x.campaign_id
   AND y.id <= x.id
 GROUP
    BY x.campaign_id
     , x.id
      ) a
JOIN     ( SELECT x.* 
     , COUNT(*) rank
  FROM campaign_charges x
  JOIN campaign_charges y
    ON y.campaign_id = x.campaign_id
   AND y.id <= x.id
 GROUP
    BY x.campaign_id
     , x.id
      ) b
     ON b.campaign_id = a.campaign_id AND b.rank = a.rank+1
   AND a.charges <> b.charges;

SQL Fiddle Demo