求一条sql语句,关于相同数据筛选的
有一个需求,现在有一个新闻的数据表(news),里面的字段是新闻id(id),栏目id(cid),新闻标题(title)和发布时间(pubdate)。
要合起来读其中3个栏目的新闻,现在用的语句是select * from news where cid=1 or cid=2 or cid=3;
不过这3个栏目的新闻会出现一个情况,有些新闻是重复的
新闻重复是指在一个时间间隔内(2天之内)标题相同的才是重复的
比如说,栏目1在2011-10-1发了一条标题叫“学院放假通知”的新闻,栏目2在2011-9-30号也发了一条“学院放假通知”的新闻,这两条新闻重复
但是在2011-9-31到2011-10-2之外的日期发的“学院放假通知”则不算和上面的重复,比如在2010年发的“学院放假通知”或者2011-9-10发的,就不算
这样子我有没有办法使用sql语句直接读出这3个栏目的新闻,并且把上面说的这种重复的新闻去掉?
[解决办法]
select distinct title ,* from news where cid=1 or cid=2 or cid=3;
[解决办法]
第5条为什么没有?测试新闻2 没有重复的?
[解决办法]
为什么没有
(7, 4, '测试新闻4', '2011-12-02'),
[解决办法]
- SQL code
mysql> select * from test;+----+------+--------------+------------+| id | cid | title | pubdate |+----+------+--------------+------------+| 1 | 1 | 测试通知新闻 | 2011-12-05 || 2 | 2 | 测试通知新闻 | 2011-12-05 || 3 | 3 | 测试通知新闻 | 2011-12-04 || 4 | 1 | 测试新闻1 | 2011-12-04 || 5 | 2 | 测试新闻2 | 2011-12-04 || 6 | 3 | 测试新闻3 | 2011-12-03 || 7 | 4 | 测试新闻4 | 2011-12-02 || 8 | 2 | 测试新闻5 | 2011-12-02 || 9 | 1 | 测试新闻6 | 2011-12-02 || 10 | 2 | 测试新闻7 | 2011-12-01 || 11 | 3 | 测试通知新闻 | 2011-12-01 || 12 | 2 | 测试通知新闻 | 2011-11-30 || 13 | 3 | 测试新闻9 | 2011-11-30 || 14 | 1 | 测试新闻10 | 2011-11-30 || 15 | 4 | 测试新闻12 | 2011-11-28 || 16 | 2 | 测试通知新闻 | 2011-11-27 |+----+------+--------------+------------+16 rows in set (0.01 sec)mysql> select * -> from test a -> where cid in (1,2,3) -> and not exists ( -> select 1 from test -> where id<a.id and title=a.title -> and pubdate between a.pubdate-interval 2 day and a.pubdate+interval 2 day -> );+----+------+--------------+------------+| id | cid | title | pubdate |+----+------+--------------+------------+| 1 | 1 | 测试通知新闻 | 2011-12-05 || 4 | 1 | 测试新闻1 | 2011-12-04 || 5 | 2 | 测试新闻2 | 2011-12-04 || 6 | 3 | 测试新闻3 | 2011-12-03 || 8 | 2 | 测试新闻5 | 2011-12-02 || 9 | 1 | 测试新闻6 | 2011-12-02 || 10 | 2 | 测试新闻7 | 2011-12-01 || 11 | 3 | 测试通知新闻 | 2011-12-01 || 13 | 3 | 测试新闻9 | 2011-11-30 || 14 | 1 | 测试新闻10 | 2011-11-30 || 16 | 2 | 测试通知新闻 | 2011-11-27 |+----+------+--------------+------------+11 rows in set (0.00 sec)mysql>