1

我有一个帮助台电话列表,我正在尝试创建一个旧电话列表,即我们已经超过 2 个月没有收到他们的来电。

我试图按照以下方式做一些事情:

select company, closedate 
from opencall 
where closedate < date sub (curdate(), interval 60 day) 
group by company

但这不起作用。

如何返回 2 个月或更长时间没有消息的公司的结果集?

4

1 回答 1

0

试试这个查询 -

(编辑示例)

CREATE TABLE opencall(
  company INT(11) DEFAULT NULL,
  closedate DATE DEFAULT NULL
);

-- Populate table, only company=4 has old reecords.
INSERT INTO opencall VALUES 
  (1, '2012-09-18'),
  (1, '2012-06-05'),
  (1, '2012-08-18'),
  (2, '2012-09-13'),
  (2, '2012-08-04'),
  (4, '2012-05-12'),
  (4, '2012-04-11');

SELECT t1.company, t1.closedate FROM opencall t1
  JOIN (SELECT company, MAX(closedate) max_closedate
       FROM opencall
       GROUP BY company
       HAVING max_closedate < CURDATE() - INTERVAL 60 DAY
       ) t2
    ON t1.company = t2.company;

+---------+------------+
| company | closedate  |
+---------+------------+
|       4 | 2012-05-12 |
|       4 | 2012-04-11 |
+---------+------------+
于 2012-09-18T11:47:46.203 回答