sql2005怎么实现mysql中的if()函数

xharvard 2009-05-28 11:58:06
mysql中的IF(expr1,expr2,expr3)
如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3。

在sql2005中要实现相同功能该怎么写?
...全文
546 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2009-05-29
  • 打赏
  • 举报
回复

MS SQL Server 中你可以用 case when expr1 then expr2 else expr3 end



USE AdventureWorks;
GO
SELECT ProductNumber, Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO
xharvard 2009-05-29
  • 打赏
  • 举报
回复
可以了。谢谢了!!!
ACMAIN_CHM 2009-05-29
  • 打赏
  • 举报
回复

SQL server 中直接这样就行了。
1> select * from course
2> go
course_id
---------
000001
000002
000003
000004
000005

(5 rows affected)
1> select * from elective
2> go
username course_id
-------- ---------
aaaaaa 000001
bbbbbb 000002
cccccc 000001
dddddd 000001
eeeeee 000003

(5 rows affected)

1> select c.course_id,count(e.course_id)
2> from course c left join elective e on c.course_id=e.course_id
3> group by c.course_id
4> go
course_id
--------- -----------
000001 3
000002 1
000003 1
000004 0
000005 0

1>
xharvard 2009-05-29
  • 打赏
  • 举报
回复
对的。有一张表专门放课程信息的。

String sQuery = "select course.*,IF(elective.course_id, count(*), 0) as amount "
+ "from course left join elective "
+ "on course.course_id = elective.course_id "
+ "where course.course_id not in "
+ "(select distinct course_id from elective where username='" + sUsername + "') "
+ "group by course.course_id";


其中course表是课程信息表。这个代码是用Mysql写的。我现在要用sql2005实现,其他都没问题,就是IF(elective.course_id, count(*), 0) as amount 在sql2005里没这么个函数,
ACMAIN_CHM 2009-05-29
  • 打赏
  • 举报
回复

course_id ,这样,你应该还有一张表吧。专门放所有课程的。

另外你的程序是如何通知SQL 要列三个课程号?而把 000003,0000004剔除?你的输入是什么?
000001 3
000002 1
000005 0
xharvard 2009-05-29
  • 打赏
  • 举报
回复
但是,我要让结果在一列里显示出来,而不是每次都用一条语句

course_id count
-------------------
000001 3
000002 1
000005 0

ACMAIN_CHM 2009-05-29
  • 打赏
  • 举报
回复

这个好象根本不需要用到IF啊,你直接select count(*)就行了。测试如下。

[code=BatchFile]1> select * from elective
2> go
username course_id
-------- ---------
aaaaaa 000001
bbbbbb 000002
cccccc 000001
dddddd 000001
eeeeee 000003

(5 rows affected)
1> select count(*) from elective where course_id='000001'
2> go

-----------
3

(1 rows affected)
1> select count(*) from elective where course_id='000002'
2> go

-----------
1

(1 rows affected)
1> select count(*) from elective where course_id='000005'
2> go

-----------
0

(1 rows affected)
1>
[/code]
xharvard 2009-05-29
  • 打赏
  • 举报
回复
表elective(选课表)中有两个字段username(用户名)和course_id(课程号)
如果查询表的时候课程号存在,则把该课程号对应的所有记录统计出来,可以用count(*),如果课程号不存在,则返回0。

username course_id
-----------------------
aaaaaa 000001
bbbbbb 000002
cccccc 000001
dddddd 000001
eeeeee 000003


如果课程号是000001 则返回 3
如果课程号是000002 则返回 1
如果课程号是000005 则返回 0
ACMAIN_CHM 2009-05-29
  • 打赏
  • 举报
回复

[Quote]我不是这个意思...我的意思是如果表达式1为true(即这个字段值存在),则执行表达式2,否则执行表达式3。[/Quote]

举个例子说明一下呢,否则越听越糊涂。
xharvard 2009-05-29
  • 打赏
  • 举报
回复
我不是这个意思...我的意思是如果表达式1为true(即这个字段值存在),则执行表达式2,否则执行表达式3。

2,596

社区成员

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

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