是否可以将MySQL中select exists查询的值分配给存储过程中的多个变量?

时间:2017-10-18 17:44:53

标签: mysql

set sender_id =(选择EXISTS(SELECT 1 FROM account WHERE account_id = p_sender_id));

set viewer_id =(选择EXISTS(SELECT 1 FROM account WHERE account_id = p_viewer_id));

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

SELECT EXISTS(SELECT 1 FROM account WHERE account_id = p_sender_id)),
       EXISTS(SELECT 1 FROM account WHERE account_id = p_viewer_id))
INTO sender_id, viewer_id;

详细了解SELECT...INTO

重新评论:

如果要运行一个子查询来设置两者:

SELECT
  MAX(CASE account_id WHEN p_sender_id THEN 1 ELSE 0 END),
  MAX(CASE account_id WHEN p_viewer_id THEN 1 ELSE 0 END)
INTO sender_id, viewer_id 
FROM account
WHERE account_id IN (p_sender_id, p_viewer_id);