56,677
社区成员
发帖
与我相关
我的任务
分享
mysql> select * from t3;
+-----+-----+---------------------+
| fid | tid | logtime |
+-----+-----+---------------------+
| 3 | 5 | 2011-03-02 00:00:00 |
| 3 | 3 | 2011-03-02 00:00:00 |
| 3 | 4 | 2011-03-02 00:00:00 |
| 3 | 5 | 2011-03-02 00:00:00 |
| 3 | 6 | 2011-03-02 00:00:00 |
| 3 | 7 | 2011-03-02 00:00:00 |
| 3 | 3 | 2011-03-02 00:00:00 |
| 3 | 4 | 2011-03-02 00:00:00 |
| 3 | 5 | 2011-03-02 00:00:00 |
| 3 | 6 | 2011-03-02 00:00:00 |
| 3 | 7 | 2011-03-02 00:00:00 |
| 3 | 8 | 2011-03-02 00:00:00 |
| 4 | 1 | 2011-03-02 00:00:00 |
| 4 | 2 | 2011-03-02 00:00:00 |
| 4 | 5 | 2011-03-02 00:00:00 |
+-----+-----+---------------------+
15 rows in set (0.00 sec)
mysql> select * from
-> (
-> select `fid`, `tid`, count(*) as cnt
-> from t3
-> group by `fid`, `tid`
-> ) a
-> where 3>(
-> select count(*) from (
-> select `fid`, `tid`, count(*) as cnt
-> from t3
-> group by `fid`, `tid`
-> ) b where fid=a.fid and (cnt>a.cnt or cnt=a.cnt and tid<a.tid)
-> )
-> order by 1,3 desc,2;
+-----+-----+-----+
| fid | tid | cnt |
+-----+-----+-----+
| 3 | 5 | 3 |
| 3 | 3 | 2 |
| 3 | 4 | 2 |
| 4 | 1 | 1 |
| 4 | 2 | 1 |
| 4 | 5 | 1 |
+-----+-----+-----+
6 rows in set (0.00 sec)
mysql>
select * from
(
select fid, tid, count(*) as cnt from t3 group by fid,`tid`
) a
where 3>=(
select count(*) from (
select `fid`, `tid`, count(*) as cnt
from t3
group by `fid`, `tid`
) b where fid=a.fid and cnt>=a.cnt
)
create temporary table temp
select fid,id,count(*) as num
from t3
group by fid,id
select *
from temp A
where (select count(*) from temp B where A.fid = B.fid and A.num<=B.num)<=50
select * from
(
select `fid`, `tid`, count(*) as cnt
from t3
where group by fid`, `tid`
) a
where 50>=(
select count(*) from (
select `fid`, `tid`, count(*) as cnt
from t3
group by `fid`, `tid`
) b where fid=a.fid and cnt>=a.cnt
)