一个查询,有没有好的想法

dxlqiu 2013-02-19 05:44:06
create table mj_test(id int not null auto_increment,country varchar(100) not null default '',register_time char(6) not null default '',primary key(id))

表如上:
想要查出每个国家在每个存在的时间 之前的个数。
比如说
表里有数据
1 china 20130101
2 china 20130102
3 Australia 20130101
4 Japan 20130204
5 china 20130314
6 Japan 20130315

想得到的数据为
country date number
china 20130101 1
china 20130102 2
china 20130314 3
Australia 20130101 1
Japan 20130204 1
Japan 20130315 2

即获得每个国家所存在的时间 之前的当前国家的个数
请大家指教
...全文
82 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dxlqiu 2013-02-19
  • 打赏
  • 举报
回复
引用 2 楼 dxlqiu 的回复:
引用 1 楼 rucypli 的回复:select country,date,(select count(*) from tb where A.country=country and A.id>=id) from tb A 这个不对吧,结果china都有5条记录了?
是我看错了,应该是对的
dxlqiu 2013-02-19
  • 打赏
  • 举报
回复
引用 1 楼 rucypli 的回复:
select country,date,(select count(*) from tb where A.country=country and A.id>=id) from tb A
这个不对吧,结果china都有5条记录了?
rucypli 2013-02-19
  • 打赏
  • 举报
回复
select country,date,(select count(*) from tb where A.country=country and A.id>=id) from tb A

56,675

社区成员

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

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