2015-06-24 82 views
0

我有一個這樣的查詢存在差異和EXISTS

SELECT ... FROM ... WHERE (SELECT EXISTS (SELECT...)) 

which did not return anything then i changed it to 

    SELECT ... FROM ... WHERE (EXISTS (SELECT...)) 

and i got my results, can anyone explain the difference? 

這裏完整的查詢,變更爲第40行:

SELECT 
    user_element_status.user_id, 
    user_element_status.element_id, 
    notifications.id 
FROM 
    notifications 
    INNER JOIN notification_element ON (notification_element.notification_id=notifications.id) 
    INNER JOIN user_element_status ON ( 
             user_element_status.element_id=notification_element.element_id 
            AND 
             case 
              when notifications.notstatus=0 then notifications.notification_status LIKE CONCAT('%',user_element_status.`status`,'%') 
              when notifications.notstatus=1 then notifications.notification_status NOT LIKE CONCAT('%',user_element_status.`status`,'%') 
             End 
            AND user_element_status.`status` != 'not_required' 
            AND 
             DATE_ADD(
             case 
              when notifications.notification_trigger='assigned' then user_element_status.assigned 
              when notifications.notification_trigger='first_launch' then user_element_status.firstlaunch 
              when notifications.notification_trigger='completed' then user_element_status.completedate 
              when notifications.notification_trigger='due' then user_element_status.duedate 
              when notifications.notification_trigger='credited' then user_element_status.creditcompletedate 
             End 
             , INTERVAL triggerdelay DAY) 
             - CURRENT_TIMESTAMP < 0 
            ) 
    INNER JOIN users ON (users.id=user_element_status.user_id AND users.is_active=1) 


WHERE 
    NOT EXISTS ( 
        SELECT * FROM alreadysent 
        WHERE 
         alreadysent.user_id=user_element_status.user_id AND 
         alreadysent.element_id=notification_element.element_id AND 
         alreadysent.notification_id=notification_element.notification_id 
       ) 
    AND 
    (EXISTS (
     SELECT 
      user_group.user_id, 
      element_group.element_id 
     FROM user_group 
      user_group 
      INNER JOIN users ON (users.id=user_group.user_id) 
      LEFT JOIN element_group ON (element_group.group_id=user_group.group_id) 
      LEFT JOIN elements ON (elements.group_id=user_group.group_id) 
      LEFT JOIN element_localization ON ((element_localization.element_id=element_group.element_id OR element_localization.element_id=elements.id) AND users.country_id=element_localization.country_id) 
      LEFT JOIN element_arealocalisation ON (element_arealocalisation.element_id = element_group.element_id OR element_arealocalisation.element_id=elements.id) 
      LEFT JOIN area_country ON (element_arealocalisation.area_id = area_country.area_id AND area_country.country_id=users.country_id) 
     WHERE 
      users.is_active=1 AND 
      user_group.user_id=user_element_status.user_id AND 
      (element_group.element_id=user_element_status.element_id OR elements.id=user_element_status.element_id) AND 
      (element_localization.country_id IS NOT NULL OR area_country.country_id IS NOT NULL) 
     )) 
+0

如果沒有真正的查詢和模式細節,無法回答。 –

+0

將看到確切的查詢很有用 - 我試過快速測試並得到相同的結果 – PaulF

+0

我已經添加了查詢 – wutzebaer

回答

1

您是否檢查了Documentation;實際的語法是

SELECT .. FROM .. WHERE [NOT]EXISTS (Subquery); 

每在後您的編輯,你實際上是在做下面這是不是你在第一個查詢

WHERE 
    NOT EXISTS ( subquery) 
    AND 
    EXISTS (subquery) 

見@siride評論顯示什麼,唯一的區別我可以說是的AND (EXISTS (subquery))情況下,如果子查詢返回的行那麼它評價爲AND TRUE,而在AND (SELECT EXISTS (subquery))情況下,它會評估爲AND SELECT TRUE ... 在兩種情況下EXISTS邏輯應該很好地工作,你的查詢應返回相同的預期結果設置,但根據你的帖子,你說第一個版本AND (SELECT EXISTS (subquery))不提取記錄......我真的懷疑。

+0

是的,但兩個版本的工作沒有錯誤 – wutzebaer

+0

這很奇怪,因爲第一個查詢肯定會拋出其他RDBMS語法錯誤,如'MSSQL' – Rahul

+0

是的,但在MySQL上它工作,我不確定我的修復是否正確 – wutzebaer