如何避免执行子查询?

时间:2016-05-27 17:17:46

标签: mysql sql subquery

我有这样的查询:

 IF( @reputation < 50
     AND NEW.user_id != NEW.author_id
     AND (NEW.table_code != 15
          OR new.user_id != (SELECT
                                CASE WHEN a.related IS NULL THEN a.author_id 
                                     ELSE b.author_id END cols
                              FROM qanda a
                              LEFT JOIN qanda b ON a.related = b.id
                              WHERE a.id = NEW.post_id )
         )

   ) THEN

如您所知,将首先执行子查询SELECT语句)。我的意思是MySQL将执行SELECT然后评估条件。因为子查询SELECT语句)是括号内的。

现在我试图避免先执行该子查询。我希望MySQL首先评估所有其他条件,然后如果所有条件都是TRUE则执行该子查询。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

怎么样:

IF @reputation < 50
   AND NEW.user_id != NEW.author_id
THEN
    IF NEW.table_code != 15
       OR new.user_id !=
                         (
                          SELECT CASE
                                     WHEN a.related IS NULL
                                     THEN a.author_id
                                     ELSE b.author_id
                                 END cols
                          FROM qanda a
                          LEFT JOIN
                          qanda b
                          ON a.related = b.id
                          WHERE a.id = NEW.post_id
                         )
        THEN