SELECT EXISTS和EXISTS之间的区别

时间:2015-06-24 14:22:36

标签: mysql

我有一个像这样的查询

    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)
        ))

1 个答案:

答案 0 :(得分: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 ... BUT 在任何一种情况下,AND (SELECT EXISTS ( subquery ))逻辑都可以正常工作,您的查询应该返回相同的预期结果集,但根据您的帖子,您说第一个版本public class MyScoreProvider extends CustomScoreProvider { private FieldCache.Ints priorities; public MyScoreProvider(AtomicReaderContext context) throws IOException { super(context); priorities = FieldCache.DEFAULT.getInts(context.reader(), "priority_numeric", false); } public float customScore(int doc, float subQueryScore, float valSrcScore) { int segment = 100 / getSegmentNumber(subQueryScore); return segment + priorities.get(doc); } } 无法获取记录......我对此非常怀疑。