0

我有 2 个表 Clients 和 Notes,在 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 
4

2 回答 2

1

基本原则是您需要按客户对您的笔记表进行分组,然后过滤那些组(即 use HAVING)仅用于最近日期在 30 天前之前的那些:

SELECT   NtClient
FROM     `client-notes`
GROUP BY NtClient
HAVING   MAX(NtWhen) < DATESUB(NOW(), 30)

clients如果您需要公司名称等,您可以像以前一样加入您的桌子。

于 2012-05-10T13:49:04.090 回答
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;
于 2012-05-10T13:49:08.787 回答