开发者社区 问答 正文

一个mysql查询的问题,纠结了好久了。

问题描述如下:
想要查询4个月内发表文章最多的3个用户(用户当然不能重复),当然文章得是最新的,因为页面上的列表显示是用户名和最新的文章标题

php代码如下:

$m4 = 当前时间戳 - 86400 * 124;


具体sql如下:
select uid,uname,title 表名 where dateline>$m4 group by uid order by dateline desc


虽然这样我能够查询出来4个月内不重复的用户,但是文章标题却是这个用户发表的第一篇文章,不是最后发布的文章。

另外需要注意的是:
1、不能使用联合查询
2、不能使用子查询  


T-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 values(‘标题1’, 2, ‘用户2’, 1436708324);
insert article values(‘标题2’, 2, ‘用户2’, 1438515690);
insert article values(‘标题3’, 2, ‘用户2’, 1438608818);
insert article values(‘标题4’, 1, ‘用户1’, 1436458649);
insert article values(‘标题5’, 2, ‘用户2’, 1437273021);
insert article values(‘标题6’, 2, ‘用户2’, 1438687437);


展开
收起
胖男孩 2015-10-10 15:34:39 4509 分享 版权
2 条回答
写回答
取消 提交回答
  • 一个程序员,欢迎骚扰!!!
    desc 降序是没有问题的 你的时间类型是int 这样也么有问题的 是不是lz看错了
    2015-10-12 11:01:55
    赞同 展开评论
  • 码农|Coder| Pythonista
    您好
    您使用desc排序出错,不知您是否测试过asc?
    2015-10-12 03:26:24
    赞同 展开评论