SQL 中case语句

LIMINLY520 2013-04-16 10:48:51
下面是我的sql语句
select No,
(case Data_Type
when '1' then BodyDate_T.Body_Data end) as type1,
(case Data_Type
when '2' then BodyDate_T.Body_Data end) as type2,
(case Data_Type
when '3' then BodyDate_T.Body_Data end) as type3,
BodyDate_T.UpdateDatetime
from BodyDate_T
order by BodyDate_T.UpdateDatetime

执行的结果如下
No type1 type2 type3 UpdateDatetime
201 NULL 32 NULL 2010-01-10 08:42:05
202 NULL NULL NULL 2010-01-10 08:42:05
203 42 NULL NULL 2010-01-10 08:42:05
204 NULL NULL 50 2010-01-10 08:42:35

怎样才能将三者都不符合的数据不显示(想要的结果如下)
No type1 type2 type3 UpdateDatetime
201 NULL 32 NULL 2010-01-10 08:42:05
203 42 NULL NULL 2010-01-10 08:42:05
204 NULL NULL 50 2010-01-10 08:42:35
...全文
76 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2013-04-16
  • 打赏
  • 举报
回复
where type1 is not null or type2 is not null or type3 is not null
LIMINLY520 2013-04-16
  • 打赏
  • 举报
回复
引用 1 楼 WWWWA 的回复:
SELECT * FROM ( select No, (case Data_Type when '1' then BodyDate_T.Body_Data end) as type1, (case Data_Type when '2' then BodyDate_T.Body_Data end) as ty……
试好了,应该是用or而不是and 谢谢了啊
LIMINLY520 2013-04-16
  • 打赏
  • 举报
回复
引用 1 楼 WWWWA 的回复:
SELECT * FROM ( select No, (case Data_Type when '1' then BodyDate_T.Body_Data end) as type1, (case Data_Type when '2' then BodyDate_T.Body_Data end) as ty……
对不起 ,我看错了,我又试了一下,结果一条数据都没有了
LIMINLY520 2013-04-16
  • 打赏
  • 举报
回复
引用 1 楼 WWWWA 的回复:
SELECT * FROM ( select No, (case Data_Type when '1' then BodyDate_T.Body_Data end) as type1, (case Data_Type when '2' then BodyDate_T.Body_Data end) as ty……
不行,报出列名'type1'无效
WWWWA 2013-04-16
  • 打赏
  • 举报
回复
SELECT * FROM ( select No, (case Data_Type when '1' then BodyDate_T.Body_Data end) as type1, (case Data_Type when '2' then BodyDate_T.Body_Data end) as type2, (case Data_Type when '3' then BodyDate_T.Body_Data end) as type3, BodyDate_T.UpdateDatetime from BodyDate_T) a where type1 is not null and type2 is not null type3 is not null order by BodyDate_T.UpdateDatetime

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧