mysql查询:发疯了

时间:2010-10-22 10:32:32

标签: mysql

我在这样的表上运行查询时遇到了一些问题:

+-----+---------------------+-------+------+
| id  | paid_date           | amount| type |
+-----+---------------------+-------+------+
| 204 | 2010-10-22 05:12:54 |  1000 |    0 |
| 205 | 2010-10-22 05:13:12 |  1000 |    1 |
| 206 | 2010-10-21 05:13:44 |  1000 |    0 |
| 208 | 2010-10-22 05:57:33 |  1000 |    1 |
+-----+---------------------+-------+------+

“类型”列确定资金是否进入,因此我想运行一个可以给我这个结果的查询

+---------------------+-------+------+
| DATE(paid_date)     | in    | out  |
+---------------------+-------+------+
| 2010-10-21          |  1000 |    0 |
| 2010-10-22          |  1000 | 2000 |
+---------------------+-------+------+

我不知道我做错了什么,我知道这不是那么困难,但却无法实现:(

1 个答案:

答案 0 :(得分:2)

   SELECT DATE(paid_date), 
   SUM(CASE type when 0 then amount else 0 end) in,
   SUM(CASE type when 1 then amount else 0 end) out
   FROM Table
   GROUP BY DATE(paid_date)