当它们具有相同的FROM时,一个SELECT语句而不是两个

时间:2016-05-03 03:36:35

标签: mysql sql

我有这个问题:

IF( 125 > (SELECT reputation FROM users WHERE id=new.user_id) OR
    1  <> (SELECT active FROM users WHERE id=new.user_id)
) THEN
   SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "You need at least 125rep to give downvote.
                                               And your account have to be active.";
END IF;

如何在IF声明中改善该条件?

1 个答案:

答案 0 :(得分:3)

我们可以合并,如下:

SELECT 1 
FROM users 
WHERE id=new.user_id 
AND (reputation < 125 OR (active != 1 OR active IS NULL))

编辑:忽略空活动状态。