Shafizadeh Shafizadeh - 7 months ago 11
SQL Question

One SELECT statement instead of two when they have identical FROM

I have this query:

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;


How can I improve that condition in the
IF
statement?

Answer

We can maybe combine, as follows:

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