mysql:比较同一个表上的两行

时间:2014-10-07 11:54:56

标签: mysql sql

我输出如:

+-----------+------------+--------------+
| client_id | date       | spends       |
+-----------+------------+--------------+
|        57 | 2014-09-28 | 39576.384391 |
|        57 | 2014-10-05 | 22664.382575 |
+-----------+------------+--------------+

我需要这样的行,如果前一周的花费是空的,那么当前的一周花费了。

1 个答案:

答案 0 :(得分:1)

这是你需要的吗?

select t.*
from table t
where spend is not null and
      exists (select 1
              from table t2
              where t2.client_id = t.client and
                    t2.spends is null and
                    t2.date = t.date - interval 7 days
             );