选择陈述中的案例 [英] Case in Select Statement

查看:71
本文介绍了选择陈述中的案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条SQL语句,该语句的CASE来自SELECT,但我无法正确执行.你们能给我看一个CASE的例子吗,其中案例是条件,结果是案例的结果.例如:

I have an SQL statement that has a CASE from SELECT and I just can't get it right. Can you guys show me an example of CASE where the cases are the conditions and the results are from the cases. For example:

     Select xxx, yyy
     case : desc case when bbb then 'blackberry';
     when sss then 'samsung';
     end 
     from (select ???? .....

结果显示

 name                         age       handphone
xxx1                         yyy1      blackberry
xxx2                         yyy2      blackberry

推荐答案

对于有关语法和用法的这类问题,MSDN是一个很好的参考.这是从《 Transact SQL参考-案例》页面获得的.

The MSDN is a good reference for these type of questions regarding syntax and usage. This is from the Transact SQL Reference - CASE page.

http://msdn.microsoft.com/en-us/library/ms181765.aspx

USE AdventureWorks2012;
GO
SELECT   ProductNumber, Name, "Price Range" = 
  CASE 
     WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
     WHEN ListPrice < 50 THEN 'Under $50'
     WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
     WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
     ELSE 'Over $1000'
  END
FROM Production.Product
ORDER BY ProductNumber ;
GO

SQL Server Central ,是您可能想查看是否正在使用SQL Server的另一个不错的站点.对于您想学习的任何SQL Server领域,它都有大量可用的资源.

Another good site you may want to check out if you're using SQL Server is SQL Server Central. This has a large variety of resources available for whatever area of SQL Server you would like to learn.

这篇关于选择陈述中的案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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