日期:2014-05-18  浏览次数:20376 次

简单SQL问题请教!
今天面试了一家公司,有一道SQL问题做不出来,请教大家:
有个表,表里有个字段,字段的数值为0,1   两种.查询的时候如果是1就返回男,如果是0就返回女.即用SQL语句的时候返回男女,而不是返回01.
我一看觉得容易,想一下,做不出来.请教了

------解决方案--------------------
select CASE sex WHEN 1 THEN '男 ' ELSE '女 ' END from ....
------解决方案--------------------
select *, 'a ' = case 字段=1 then '男 ' else '女 ' end from 表
------解决方案--------------------
示例
A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。

USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp ' THEN 'Popular Computing '
WHEN 'mod_cook ' THEN 'Modern Cooking '
WHEN 'business ' THEN 'Business '
WHEN 'psychology ' THEN 'Psychology '
WHEN 'trad_cook ' THEN 'Traditional Cooking '
ELSE 'Not yet categorized '
END,
CAST(title AS varchar(25)) AS 'Shortened Title ',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO

下面是结果集:

Category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive 's Data 19.99
Business Straight Talk About Compu 19.99

avg
==========================
13.73

Category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99

avg
==========================
11.49

Category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User Friendly? 22.95

avg
==========================
21.48

Category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology Life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59

avg
==========================