2016-05-27 139 views
1

我有這樣的查詢:如何避免執行子查詢?

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,則執行該子查詢。我怎樣才能做到這一點?

回答

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 
+0

仍然是子查詢到一個括號,我認爲這將首先執行。 –

+0

從我理解的第一個IF語句會得到執行然後如果該條件通過第二個 –

+0

啊我希望如此.. thx無論如何.. +1 –