就是想要查询4个月内发表文章最多的3个用户(用户当然不能重复),当然文章得是最新的,因为页面上的列表显示是用户名和最新的文章标题
php代码如下:
<code class="php">$m4 = 当前时间戳 - 86400 * 124;</code>
具体sql如下:
<code class="sql">select uid,uname,title 表名 where dateline>$m4 group by uid order by dateline desc</code>
虽然这样我能够查询出来4个月内不重复的用户,但是文章标题却是这个用户发表的第一篇文章,不是最后发布的文章。
另外需要注意的是:
1、不能使用联合查询
2、不能使用子查询
T-SQL:
<code class="sql">create table article( `id` int(11) unsigned not null auto_increment comment '编号id', `subject` varchar(300) not null comment '标题', `uid` mediumint(8) unsigned not null comment '用户编号', `uname` varchar(20) not null comment '用户名', `dateline` int(10) unsigned not null comment '发表时间', primary key(id)) engine=myisam charset=utf8 comment='文章信息表'; insert article(subject, uid, uname, dateline)values('标题1', 2, '用户2', 1436708324),('标题2', 2, '用户2', 1438515690),('标题3', 2, '用户2', 1438608818),('标题4', 1, '用户1', 1436458649),('标题5', 2, '用户2', 1437273021),('标题6', 2, '用户2', 1438687437);</code>
本文来源gao@!dai!ma.com搞$$代^@码!网!搞gaodaima代码
回复内容:
就是想要查询4个月内发表文章最多的3个用户(用户当然不能重复),当然文章得是最新的,因为页面上的列表显示是用户名和最新的文章标题
php代码如下:
<code class="php">$m4 = 当前时间戳 - 86400 * 124;</code>
具体sql如下:
<code class="sql">select uid,uname,title 表名 where dateline>$m4 group by uid order by dateline desc</code>
虽然这样我能够查询出来4个月内不重复的用户,但是文章标题却是这个用户发表的第一篇文章,不是最后发布的文章。
另外需要注意的是:
1、不能使用联合查询
2、不能使用子查询
T-SQL:
<code class="sql">create table article( `id` int(11) unsigned not null auto_increment comment '编号id', `subject` varchar(300) not null comment '标题', `uid` mediumint(8) unsigned not null comment '用户编号', `uname` varchar(20) not null comment '用户名', `dateline` int(10) unsigned not null comment '发表时间', primary key(id)) engine=myisam charset=utf8 comment='文章信息表'; insert article(subject, uid, uname, dateline)values('标题1', 2, '用户2', 1436708324),('标题2', 2, '用户2', 1438515690),('标题3', 2, '用户2', 1438608818),('标题4', 1, '用户1', 1436458649),('标题5', 2, '用户2', 1437273021),('标题6', 2, '用户2', 1438687437);</code>
思路应该是:
-
先找出四个月内发表文章最多的3个用户;
-
再根据用户id查询该用户最新的文章标题;
sql如下:
找出四个月内发表文章最多的3个用户
<code>select uid,name,count(subject) as article_count from article where dateline>$m4 group by uid order by article_count desc, dateline desc limit 3</code>
根据用户id查询该用户最新的文章标题:
<code>select subject from article where uid='uid' order by dateline desc limit 1</code>
select * from (
<code>select uid,uname,title 表名 where dateline>$m4 order by dateline DESC</code>
) a group by uid
1、不能使用联合查询
2、不能使用子查询
那只好尝试一下为 dateline
建立逆序索引
都不能那就查两次呗
自行脑补一下MySQL的having语法
我是 新手,不过group by这样写能执行吗