问一个select语句

cagecn 2004-04-08 04:54:56
要求:按照字段x分类,即group by x
得到的结果中,x字段中,相同的不超过5个

这个语句用在文章系统中,x是文章的目录id
现在取出每个目录中新的5篇文章
...全文
38 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cagecn 2004-04-09
  • 打赏
  • 举报
回复
CREATE TABLE `article` (
`articleid` int(10) unsigned NOT NULL auto_increment,
`cateid` int(10) NOT NULL default '0',
`posttime` int(10) unsigned NOT NULL default '0',
`author` varchar(50) NOT NULL default '',
`title` varchar(100) NOT NULL default '',
`description` text,
`clicktimes` int(10) unsigned NOT NULL default '0',
`rating` int(2) NOT NULL default '0',
`votes` smallint(6) NOT NULL default '0',
`ipaddress` varchar(16) NOT NULL default '',
`commentnum` smallint(8) NOT NULL default '0',
`published` enum('y','n') NOT NULL default 'n',
PRIMARY KEY (`articleid`),
KEY `postid` (`articleid`),
KEY `published` (`published`)
)
lehool 2004-04-09
  • 打赏
  • 举报
回复
select x as a from table1 group by x
while
SELECT * FROM `article` WHERE x=a order by cateid limit 5;
用循环吧!一条语句实现不了。
lehool 2004-04-08
  • 打赏
  • 举报
回复
不明白的意思!把表结构发出来。
cagecn 2004-04-08
  • 打赏
  • 举报
回复
错了是order by

SELECT *
FROM `article`
WHERE cateid
IN ( 7, 12 )
having count(*)<=5
order by cateid;

一个结果也没有
xiaosq2000 2004-04-08
  • 打赏
  • 举报
回复
select x
from table1
group by x
having count(*)<=5
loveflea 2004-04-08
  • 打赏
  • 举报
回复
用个循环吧!这样简单点!

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧