简体   繁体   中英

Mysql WHERE NOT EXISTS (query) OR (query)

Is it possible to write one query that checks if either subqueries are correct as opposed to one?

SELECT  *
FROM    employees e
WHERE   NOT EXISTS
        (
        SELECT  null 
        FROM    eotm_dyn d
        WHERE   d.employeeID = e.id
        ) OR (select * FROM table c WHERE c.employeeID = e.id)

You question is a little hard to understand but I'm guessing this is what You are asking for. It will return records from employees table that doesn't have any related rows in tables eotm_dyn and table .

SELECT  *
FROM    employees e
WHERE  e.id NOT IN
    (
        (SELECT  employeeID 
        FROM    eotm_dyn)
     UNION
        (SELECT employeeID 
        FROM table)
    )

Yes, just change that OR condition to another EXISTS expression:

SELECT  *
FROM    employees e
WHERE   NOT EXISTS
           (
             SELECT  null 
             FROM    eotm_dyn d
             WHERE   d.employeeID = e.id
           ) 
    AND NOT EXISTS
           (
             select null
             FROM   table c
             WHERE  c.employeeID = e.id
           )

Also guessing as to your intention. This will check that the record does not exist for BOTH sub-queries. I'm not sure what you mean if you mean to check one OR the other.

UPDATE: Trying to imagine again, perhaps you meant to do a OR EXISTS :

SELECT  *
FROM    employees e
WHERE   NOT EXISTS
           (
             SELECT  null 
             FROM    eotm_dyn d
             WHERE   d.employeeID = e.id
           ) 
     OR EXISTS
           (
             select null
             FROM   table c
             WHERE  c.employeeID = e.id
           )

This will find records in employees that are either NOT in eotm_dyn or ARE in table .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM