我厌倦了尝试连接到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
答案 0 :(得分:30)
这将指导您使用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;
答案 1 :(得分:1)
你的意思是
SET @var := CASE n WHEN [...] END;
(http://dev.mysql.com/doc/refman/5.6/en/user-variables.html)