select s.* from cdb_posts s where s.tid in (select distinct cdb_posts.tid from cdb_posts ORDER by dateline desc limit 5) 是这样的 我需要查询出discuz最近时间有过动态的帖子,以前用的是 select distinct cdb_posts.tid from cdb_posts ORDER by dateline desc limit 5 但是后来在论坛中做测试,在查询的数据中 “subject” 变成了空的,经过分析发现这些“subject”为空的数据,tid 列值都是一样,所以改变了sql 这种思想是对的 不过执行不成功 ,说 in 和 limit 不能同时使用。
select s.* from cdb_posts s inner join (select distinct cdb_posts.tid from cdb_posts ORDER by dateline desc limit 5) b on s.tid=b.tid
or select s.* from cdb_posts s ,(select distinct cdb_posts.tid from cdb_posts ORDER by dateline desc limit 5) b where s.tid=b.tid
+------+------+
| id | col2 |
+------+------+
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
+------+------+
6 rows in set (0.02 sec)mysql> select * from foo a where not exists (select id from foo b where a.id = b.id and b.id != 3);
+------+------+
| id | col2 |
+------+------+
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
| 3 | 美国 |
+------+------+
7 rows in set (0.00 sec)
-> 2
INSTR、FIND_IN_SET、LEFT JOIN?
是这样的 我需要查询出discuz最近时间有过动态的帖子,以前用的是
select distinct cdb_posts.tid from cdb_posts ORDER by dateline desc limit 5 但是后来在论坛中做测试,在查询的数据中 “subject” 变成了空的,经过分析发现这些“subject”为空的数据,tid 列值都是一样,所以改变了sql 这种思想是对的 不过执行不成功 ,说 in 和 limit 不能同时使用。
inner join (select distinct cdb_posts.tid from cdb_posts ORDER by dateline desc limit 5) b
on s.tid=b.tid
select s.* from cdb_posts s ,(select distinct cdb_posts.tid from cdb_posts ORDER by dateline desc limit 5) b
where s.tid=b.tid