SQL SELECT语句中嵌入多重选择,用if语句
我是这样写的,不知道哪里错了USEYGGLGOSELECTEmployeeID,Name,Address,DepartmentID=(IF(SELECTDepartmen...
我是这样写的,不知道哪里错了
USE YGGL
GO
SELECT EmployeeID,Name,Address,DepartmentID=
(
IF (SELECT DepartmentID FROM Employees)='1' BEGIN RETURN '财务部' END
IF (SELECT DepartmentID FROM Employees)='2' BEGIN RETURN '人力资源部' END
IF (SELECT DepartmentID FROM Employees)='3' BEGIN RETURN '经理办公室' END
IF (SELECT DepartmentID FROM Employees)='4' BEGIN RETURN '研发部' END
IF (SELECT DepartmentID FROM Employees)='5' BEGIN RETURN '市场部' END
)
FROM Employees
使用CASE对Employees表按部门进行分类:
USE YGGL
GO
SELECT EmployeeID ,Name,Address,DepartmentID=
CASE DepartmentID
WHEN 1 THEN ‘财务部’
WHEN 2 THEN ‘人力资源部’
WHEN 3 THEN ‘金理办公室’
WHEN 4 THEN ‘研发部’
WHEN 5 THEN ‘市场部’
END
FROM Employees
练习: 使用IF语句实现上述功能 展开
USE YGGL
GO
SELECT EmployeeID,Name,Address,DepartmentID=
(
IF (SELECT DepartmentID FROM Employees)='1' BEGIN RETURN '财务部' END
IF (SELECT DepartmentID FROM Employees)='2' BEGIN RETURN '人力资源部' END
IF (SELECT DepartmentID FROM Employees)='3' BEGIN RETURN '经理办公室' END
IF (SELECT DepartmentID FROM Employees)='4' BEGIN RETURN '研发部' END
IF (SELECT DepartmentID FROM Employees)='5' BEGIN RETURN '市场部' END
)
FROM Employees
使用CASE对Employees表按部门进行分类:
USE YGGL
GO
SELECT EmployeeID ,Name,Address,DepartmentID=
CASE DepartmentID
WHEN 1 THEN ‘财务部’
WHEN 2 THEN ‘人力资源部’
WHEN 3 THEN ‘金理办公室’
WHEN 4 THEN ‘研发部’
WHEN 5 THEN ‘市场部’
END
FROM Employees
练习: 使用IF语句实现上述功能 展开
5个回答
展开全部
SELECT EmployeeID,Name,Address,
case when DepartmentID ='1' then '财务部'
when DepartmentID ='2' then '人力资源部'
when DepartmentID = '3' then '经理办公室'
when DepartmentID ='4' then '研发部'
when DepartmentID = '5' then '市场部' END as DepartmentID
FROM Employees
case when DepartmentID ='1' then '财务部'
when DepartmentID ='2' then '人力资源部'
when DepartmentID = '3' then '经理办公室'
when DepartmentID ='4' then '研发部'
when DepartmentID = '5' then '市场部' END as DepartmentID
FROM Employees
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用case,这样写就可以了
USE YGGL
GO
SELECT EmployeeID,Name,Address,
Department=
(
case DepartmentID
when '1' then '财务部'
when '2' then '人力资源部'
when '3' then '经理办公室'
when '4' then '研发部'
when '5' then '市场部'
end
)
FROM Employees
如果DepartmentID是int,那1,2,3,4,5不要加引号
USE YGGL
GO
SELECT EmployeeID,Name,Address,
Department=
(
case DepartmentID
when '1' then '财务部'
when '2' then '人力资源部'
when '3' then '经理办公室'
when '4' then '研发部'
when '5' then '市场部'
end
)
FROM Employees
如果DepartmentID是int,那1,2,3,4,5不要加引号
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SELECT EmployeeID,Name,Address,
decode(DepartmentID ,'1' ,'财务部'
,'2' ,'人力资源部'
,'3', '经理办公室'
,'4', '研发部'
,'5','市场部')as DepartmentID
FROM Employees
decode(DepartmentID ,'1' ,'财务部'
,'2' ,'人力资源部'
,'3', '经理办公室'
,'4', '研发部'
,'5','市场部')as DepartmentID
FROM Employees
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
IF 后面要true或者false
IF EXISTS(SELECT DepartmentID FROM Employees='1') ....
看行不?
IF EXISTS(SELECT DepartmentID FROM Employees='1') ....
看行不?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不要用等号,用as或者用空格试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |