mysql - mysql中选择案例中的案例

标签 mysql case nested

我已经厌倦了尝试连接到 MSSQL,所以我要切换到 mysql。 进展缓慢。这是我目前的障碍: mssql:

create function W(m varchar(255)) returns int begin

declare @e int
set @e = (select COUNT(N) from P where N = m)

declare @t int
set @t = dbo.C(m)

return case @t 
when 0 then -1 
when 1 then
    case @e when 0 then -1 else 1 end
when 2 then
    case @e when 1 then -1 when 2 then 0 when 3 then 0 when 4 then 1 end
when 3 then 
    case @e when 1 then -1 when 2 then 1 end
when 4 then 
    case @e when 1 then -1 when 2 then 0 when 3 then 1 end
end
end

我想将其切换到 mysql。是否有有效的 mysql 方法:

select select case n when 0 then 1 when 1 then 2 end into var

? 怎么样

set var = select case n when [...] end

?

最佳答案

这将指导您使用 Inline IF and CASE statements in MySQL

片段:

SELECT CASE num_heads
           WHEN 0 THEN 'Zombie'
           WHEN 1 THEN 'Human'
           ELSE 'Alien'
       END AS race
FROM user

mysql> SET @val := CASE num_heads
                       WHEN 0 THEN 'Zombie'
                       WHEN 1 THEN 'Human'
                       ELSE 'Alien'
                   END AS race;

mysql> SELECT @val;

关于mysql - mysql中选择案例中的案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9236770/

相关文章:

mysql - 如果第一个为空,如何运行第二个 sql select?

ssl - Websphere MQ Explorer SSL 字段 "Accept only certificates with Distinguished Names matching these values field"是否区分大小写?

excel - 如何在 Excel 中组合多个嵌套 Substitute 函数?

python - 递归函数来计算嵌套列表中的所有项目?

javascript - (Ruby、Rails、Javascript)使用 javascript 窗口等管理嵌套模型...?

php - 如何检查数据库中已有的空表

php - 有没有办法在MySQL中获取自动提交的默认值?

mysql - 当值不是日期时,CASE 计数值

mysql - 将列转换为 10 位数字

php - 帮忙设计简单的2表数据库吗?