gpt4 book ai didi

函数内的 MySql Case

转载 作者:行者123 更新时间:2023-11-29 02:00:03 26 4
gpt4 key购买 nike

我正在努力实现如下所示的目标,但我完全错了——逻辑、语法和其他所有方面。你能帮帮我吗?

我正在尝试根据特定条件SET STATIC =。如果这是 PHP,我会使用 Break。我走到这一步,但没有任何效果。你能看到如何做到这一点吗?

CREATE DEFINER=`root`@`localhost` FUNCTION `caseTest`(`n` INT)
RETURNS varchar(512)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN

DECLARE x VARCHAR(1500);
DECLARE y VARCHAR(1500);
DECLARE static VARCHAR(1500);

CASE n
WHEN '14' THEN SET x = 'Place Order Link';
WHEN '01' THEN SET x = 'Cancel Order Link';
WHEN '11' THEN SET x = 'Order Cancelled - Place order link';
SET static = concat(<a href="">,x,</a>);

WHEN '00' THEN SET x = 'Order - Under Process'; #No link here
WHEN '10' THEN SET x = 'Cancel - Under Process'; #No link here
SET static = x;

ELSE SET static = 'Error generating link';
END;

set y = 'Flag Link ~ Edit Link ~ Move Link';

RETURN concat(x,y);
END

最佳答案

您想将 set 移到 case 语句之外:

  set x = case n 
WHEN '14' THEN 'Place Order Link';
WHEN '01' THEN 'Cancel Order Link';
WHEN '11' THEN 'Order Cancelled - Place order link';
WHEN '00' THEN 'Order - Under Process'; #No link here
WHEN '10' THEN 'Cancel - Under Process'; #No link here
end;
set static = (case when n in ('14', '01', '11') then concat(<a href="">,x,</a>)
when n in ('00', '10') then x
ELSE 'Error generating link'
end);

您也可以使用 if 执行此操作。 set with case 似乎更接近你原来的逻辑。

我看不出为什么上面的方法不起作用(除了可能的语法错误)。您可以尝试 if 版本:

 if n = '14' THEN x = 'Place Order Link';
elseif n = '01' THEN x = 'Cancel Order Link';
elseif n = '11' THEN x = 'Order Cancelled - Place order link';
elseif n = '00' THEN x = 'Order - Under Process'; #No link here
elseif n = '10' THEN x = 'Cancel - Under Process'; #No link here

if n in ('14', '01', '11') then static = concat(<a href="">,x,</a>);
elseif n in ('00', '10') then static = x;
ELSE static = 'Error generating link';

关于函数内的 MySql Case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17262657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com