SQL Server 2008中的条件语句 [英] conditional statements in SQL server 2008

查看:93
本文介绍了SQL Server 2008中的条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server中有一个具有以下字段的表
book_code .......书名......股票
123 ...........学习C#......... 2
222 ....................学习JAVA ... 0

我想显示以下结果
如果stock = 0则显示AVAILABLE ="YES",否则为"NO"


book_code .......书名...有库存吗?
123 ...................学习C#......... 2 ......是的
222 ....................学习JAVA ... 0 ..... NO

我了解到我可以在SQL中使用if语句,但是即使经过很多尝试,我也无法完成它.
任何帮助或信息将不胜感激
在此先感谢那些愿意提供帮助的人:)

i have a table in SQL server with following fields
book_code ....... book title...... stock
123 ...................learn C#.........2
222....................learn JAVA......0

i want to show following result
if stock =0 show AVAILABLE="YES" else "NO"


book_code ....... book title...... stock..available?
123 ...................learn C#.........2......YES
222....................learn JAVA......0 .....NO

i have learnt that i can use if statements in SQL but even after trying a lot i cant get it done.
any help or information will be appreciated
thanks in advance to those who are willing to help :)

推荐答案

使用CASE语句

Use the CASE statement

SELECT BookCode, BookTitle, Stock, 
    CASE Stock 
         WHEN 0 THEN 'NO' 
         ELSE 'YES' 
    END AS Available
FROM Books


您可以使用的一个常见关键字是
One common keyword that you can use is the CASE[^]. An example on how to use it can be seen on the link. You can also check my sample SQL script below.

SELECT book_code, 
       [book title], 
       stock,
       CASE 
          WHEN stock = 0 THEN 'YES'
          ELSE 'NO'
       END AS 'AVAILABLE'
FROM   [YOURTABLE]


使用案例可以解决您的问题

Using Case you can solve your problem

SELECT Stock, 
    CASE WHEN 
      Stock=''0'' Then ''YES'' 
      ELSE ''NO'' 
    END AS AVAILABLE
FROM Table1



-希望对您有帮助.



- Hope this can help you.


这篇关于SQL Server 2008中的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆