0

我不确定我在这里做错了什么,但我的大脑有点混乱。这是我的 SQL 语句,用于选择在日期和时间之间拨打电话的所有员工

SELECT staff.name
FROM staff

JOIN staff_prospects ON staff_prospects.staff_id = staff.id
JOIN staff_prospect_activity ON staff_prospect_activity.prospect_id = staff_prospects.id

WHERE DATE_FORMAT(staff_prospect_activity.date_of_activity, "%Y-%m-%d") BETWEEN "2013-05-31" AND "2013-05-31"

GROUP BY staff.name

这将列出所有冷遇潜在客户的员工姓名。但是我需要“反转”这个查询,以便在同一日期范围内获取所有没有这样做的员工。

谢谢你的帮助!

4

3 回答 3

3

利用LEFT JOIN

SELECT  a.name
FROM    staff a
        INNER JOIN staff_prospects b
            ON b.staff_id = a.id
        LEFT JOIN staff_prospect_activity c
            ON  c.prospect_id = b.id AND
                c.date_of_activity >= '2013-05-31' AND 
                c.date_of_activity < '2013-05-31' + INTERVAL 1 DAY
WHERE   c.prospect_id IS NULL
GROUP   BY a.name
于 2013-05-31T09:42:39.777 回答
2

使用不存在。像这样的东西应该工作:

SELECT staff.name
FROM staff

WHERE NOT EXISTS (SELECT * FROM staff_prospects 
JOIN staff_prospect_activity ON staff_prospect_activity.prospect_id = staff_prospects.id
WHERE staff_prospects.staff_id = staff.id AND
DATE_FORMAT(staff_prospect_activity.date_of_activity, "%Y-%m-%d") BETWEEN "2013-05-31" AND "2013-05-31")
于 2013-05-31T11:16:20.090 回答
0
SELECT staff.name
FROM staff

INNER JOIN staff_prospects 
ON staff_prospects.staff_id = staff.id
LEFT JOIN staff_prospect_activity 
ON staff_prospect_activity.prospect_id = staff_prospects.id

WHERE DATE_FORMAT(staff_prospect_activity.date_of_activity, "%Y-%m-%d") BETWEEN "2013-    05-01" AND "2013-05-31" AND STAFF_PROSPECT_ACTIVITY.prospect_id IS NULL

GROUP BY staff.name

(您的日期与 2013 年 5 月 31 日和 2013 年 5 月 31 日相同。除非您只想要一个日期。)还可以使用左连接并检查空值。

于 2013-05-31T12:17:03.080 回答