简体   繁体   English

Mysql 案例:THEN 中有多个值

[英]Mysql CASE: Multiple values in THEN

I have to pass multiple values in THEN .我必须在THEN传递多个值。

Here is my code这是我的代码

select empid from emp_leave_authority where main_autority in
(
 CASE WHEN $emp_auth='577' THEN '541' END as column_1
 CASE WHEN $emp_auth='577' THEN '588' END as column_2                                                    
)

$emp_auth is ID in loop against which i am checking. $emp_auth是我正在检查的循环中的 ID。

I am getting empty set of result.我得到的结果是空的。

My purpose is, If main_autority found is 577, replace it with 541 and 588. (Give those employee whose main_autority is 541 and 588).我的目的是,如果发现 main_autority 是 577,用 541 和 588 替换它。(给那些 main_autority 是 541 和 588 的员工)。

Any ideas please任何想法请

If I understodd it correctly, please try below code.如果我理解正确,请尝试以下代码。

Create table #tmp
(
    searchvals int
)

If @myParam  = 577
Begin   
    insert into #tmp
    values (541)
    insert into #tmp
    values (588)
End
Else
Begin
    insert into #tmp
    values (@myParam)
End

select empid from emp_leave_authority where main_autority in (select searchvals from #tmp)

You are trying to do it in SQL.您正在尝试在 SQL 中执行此操作。 So, if $emp_auth is passed to procedure (Assuming) as parameter then you can do something like this所以,如果$emp_auth作为参数传递给过程(假设),那么你可以做这样的事情

create procedure testproc(IN auth VARCHAR(5))   
as
begin
select empid from emp_leave_authority 
where main_autority in 
(
CASE auth
WHEN '577' THEN concat('541',',','588')  
END 
)                                                  
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM