获取超过两个月的物品

时间:2012-09-18 11:29:19

标签: mysql

我有一个帮助台呼叫列表,我正在尝试创建一个旧呼叫列表,即我们两个多月没有收到他们的消息。

我试图按照以下方式做点什么:

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

但这不起作用。

如何退回我们未听过2个月或更长时间的公司结果集?

1 个答案:

答案 0 :(得分: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 |
+---------+------------+