Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

sql - Select语句中的案例(Case in Select Statement)

I have an SQL statement that has a CASE from SELECT and I just can't get it right.

(我有一个SQL语句,它有一个来自SELECTCASE ,我无法正确使用它。)

Can you guys show me an example of CASE where the cases are the conditions and the results are from the cases.

(你们能告诉我CASE一个例子, CASE是条件,结果来自案例。)

For example:

(例如:)

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

where the results show

(结果显示)

 name                         age       handphone
xxx1                         yyy1      blackberry
xxx2                         yyy2      blackberry
  ask by fadzli feizal translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The MSDN is a good reference for these type of questions regarding syntax and usage.

(对于语法和用法这些类型的问题,MSDN是一个很好的参考。)

This is from the Transact SQL Reference - CASE page.

(这来自Transact SQL Reference - CASE页面。)

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

(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

Another good site you may want to check out if you're using SQL Server is SQL Server Central .

(如果您正在使用SQL Server,您可能想要查看的另一个好网站是SQL Server Central 。)

This has a large variety of resources available for whatever area of SQL Server you would like to learn.

(这有很多种资源可用于您想要学习的SQL Server的任何区域。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...