我如何在MySQL中选择它

时间:2019-11-13 14:21:11

标签: mysql

我在MySQL中有两个表的设置:

schema

我如何SELECT没有会议的客户,或仅将开会时间设置为<> 0的会议

2 个答案:

答案 0 :(得分:0)

应该执行NOT EXISTS查询。
所有已召开<> 0的会议等于说未举行会议= 0:

SELECT *
FROM customers
WHERE NOT EXISTS (
    SELECT *
    FROM customar_meetings
    WHERE customar_meetings.customar_id = customers.customar_id
    AND meeting_held = 0
)

https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=d9e11e8d99dc9418c966b36d9012edb4

答案 1 :(得分:0)

详细但有效。

SELECT 
    *
FROM
    customers
WHERE
    NOT EXISTS( SELECT 
            *
        FROM
            customar_meetings
        WHERE
            customar_meetings.customar_id = customers.customar_id) 
UNION SELECT 
    *
FROM
    customers
WHERE
    (SELECT 
            COUNT(*)
        FROM
            customar_meetings
        WHERE
            customar_meetings.customar_id = customers.customar_id) = 1
        AND (SELECT 
            meeting_held
        FROM
            customar_meetings
        WHERE
            customar_meetings.customar_id = customers.customar_id
        LIMIT 1) <> 0;