mysql中选择案例中的案例

发布于 2025-01-04 03:39:35 字数 758 浏览 0 评论 0原文

我厌倦了尝试连接 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

I'm getting sick of trying to hook up to MSSQL, so I'm switching over to mysql.
It's slow progress. Here's my current stumper:
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

I'd like to switch this to mysql. Is there a valid mysql way to:

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

?
How about

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

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2025-01-11 03:39:35

这将指导您使用 内联 IF 和 CASE 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;

This will guide you in using Inline IF and CASE statements in MySQL

Snippet:

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

or

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

mysql> SELECT @val;
飞烟轻若梦 2025-01-11 03:39:35

你的意思是

SET @var := CASE n WHEN [...] END;

(http://dev.mysql.com/doc/refman/ 5.6/en/user-variables.html)

Do you mean

SET @var := CASE n WHEN [...] END;

?

(http://dev.mysql.com/doc/refman/5.6/en/user-variables.html)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文