我有2个表客户端和Notes,在notes表中我记录了客户端的所有活动。我想要做的是查询笔记表,找出哪些客户在过去30天内没有任何活动。
在昨晚和今天早上花了一大笔药水后,我找不到工作的解决方案..有什么建议吗?
SELECT `c`.`ClientID` , `c`.`CompanyName` , MAX( `NtWhen` )
FROM `clients` AS `c`
JOIN `client-notes` AS `n` ON ( `c`.`ClientID` = `n`.`NtClient` )
WHERE `NtText` = 'Viewed Client Details'
AND MAX(`NtWhen`) < '2012-04-10'
GROUP BY `NtClient`
ORDER BY MAX(`NtWhen`) DESC
答案 0 :(得分:1)
基本原则是您需要按客户端对笔记表进行分组,然后过滤这些组(即使用HAVING
),仅针对30天前最新日期的那些组:
SELECT NtClient
FROM `client-notes`
GROUP BY NtClient
HAVING MAX(NtWhen) < DATESUB(NOW(), 30)
如果您需要公司名称等,您可以像以前一样加入clients
表。
答案 1 :(得分:0)
SELECT `c`.`ClientID` , `c`.`CompanyName` , MAX( `NtWhen` )
FROM `clients` AS `c`
JOIN `client-notes` AS `n` ON ( `c`.`ClientID` = `n`.`NtClient` )
WHERE `NtText` = 'Viewed Client Details'
GROUP BY `NtClient`
HAVING MAX(`NtWhen`) < '2012-04-10'
ORDER BY MAX(`NtWhen`) DESC;