免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3807 | 回复: 3
打印 上一主题 下一主题

这样的case语句有什么问题,请高手帮忙急!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-16 10:14 |只看该作者 |倒序浏览
这样的case语句有什么问题,请高手帮忙急!!!
select a.acct_id,
(case when acct_month_id=\'200806\' then ys_charge else 0 end)  charge1,
(case when acct_month_id=\'200806\' then yh_charge else 0 end) charge2,
(case when acct_month_id=\'200813\' then ys_charge else 0 end) charge3,
(case when acct_month_id=\'200813\' then yh_charge else 0 end) charge4
from acct_charge a
where a.acct_month_id in(\'200806\',\'200813\')
and acct_id=560000004
group by a.acct_id;
返回结果是:
+-----------+---------+---------+---------+----------+
| acct_id       | charge1  | charge2  | charge3  | charge4  |
+-----------+---------+---------+---------+----------+
| 560000004 | 0       | 0       | 4415.57 | -4752.19 |
+-----------+---------+---------+---------+----------+
1 row in set

select * from  acct_charge a where acct_id=\'560000004\' and  a.acct_month_id in(\'200806\',\'200813\')
返回如下:
+------+-----------+---------------+-----------+-----------+
| id       | acct_id       | acct_month_id | ys_charge | yh_charge |
+------+-----------+---------------+-----------+-----------+
| 3000 | 560000004 | 200813        | 4415.57   | -4752.19  |
| 3004 | 560000004 | 200806        | 1048.7    | -768.79   |
+------+-----------+---------------+-----------+-----------+
2 rows in set


为什么case中用in语句只能返回最后的一个语句的值

论坛徽章:
0
2 [报告]
发表于 2008-07-16 12:48 |只看该作者
因为你group by a.acct_id

两个记录的a.acct_id是一样的。

论坛徽章:
4
处女座
日期:2013-09-01 19:30:07双鱼座
日期:2013-09-28 10:58:45双子座
日期:2014-05-23 08:32:0215-16赛季CBA联赛之八一
日期:2016-04-12 19:55:42
3 [报告]
发表于 2008-07-20 16:04 |只看该作者
如楼上说的,被你group掉了

论坛徽章:
0
4 [报告]
发表于 2008-07-27 10:16 |只看该作者
where a.acct_month_id in(\'200806\',\'200813\')

因为你的month_id类型是整型,不是字符串!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP