我有以下SQL代码:
SELECT eml AS "Email Address"
FROM emailtbl
WHERE DEPT LIKE 'E%'
我的问题是如果部门以'F'开头,我必须选择传真而不是eml。如果部门以“E”开头则选择eml,如果部门以“F”开头则选择传真
由于
答案 0 :(得分:9)
一种选择是使用UNION ALL
声明
SELECT eml AS "Entity"
FROM emailtbl
WHERE DEPT LIKE 'E%'
UNION ALL
SELECT fax AS "Entity"
FROM emailtbl
WHERE DEPT LIKE 'F%'
另一个是使用CASE
语句
SELECT CASE WHEN Dept LIKE 'E%' THEN eml ELSE fax END AS "Entity"
FROM emailtbl
WHERE DEPT LIKE 'E%' OR DEPT LIKE 'F%'
答案 1 :(得分:1)
这个怎么样?
IF (SUBSTRING(@useLike,1,1) = 'E')
BEGIN
SELECT eml AS "Email Address"
FROM emailtbl
WHERE DEPT LIKE @useLike
END
ELSE
BEGIN
SELECT fax AS "Email Address"
FROM emailtbl
WHERE DEPT LIKE @useLike
END