gpt4 book ai didi

mysql - 如何将查询结果存储到变量中并多次使用它?

转载 作者:行者123 更新时间:2023-11-29 11:19:28 24 4
gpt4 key购买 nike

我有一个查询(触发),如下所示:

BEGIN     
IF (new.edited_id IS NULL) THEN
IF (SELECT 1 FROM users WHERE id = new.author_id AND IFNULL((active & b'1000000' > 0), 0) < 1) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "you cannot ask anymore";
ELSEIF (SELECT 1 FROM users WHERE id = new.author_id AND IFNULL((active & b'10000000' > 0), 0) < 1) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "you cannot answer anymore";
END IF;
ELSE
IF (SELECT 1 FROM users WHERE id = new.author_id AND IFNULL((active & b'100000000' > 0), 0) < 1) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "you cannot edit anymore";
END IF;
END

如您所见,这些内部 IF 语句的条件几乎相同。那么我该如何改进呢?

我的意思是,如何将以下查询的结果存储到变量中:

SELECT active FROM users WHERE id = new.author_id;

然后将其用于如下条件:

IF (IFNULL((@variable & b'1000000' > 0), 0) < 1) THEN
IF (IFNULL((@variable & b'10000000' > 0), 0) < 1) THEN
IF (IFNULL((@variable & b'100000000' > 0), 0) < 1) THEN

我该如何处理?

最佳答案

声明一个 local variable 并使用 select 语句的输出来设置它。

DECLARE v_active bit(10);
SET v_active := (SELECT active FROM users WHERE id = new.author_id);

关于mysql - 如何将查询结果存储到变量中并多次使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39186735/

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