我要对某个表进行查询,同时要对某个字段出现的次数进行统计:
表:
id   name  money  
1    jack   3000
2    tom    5000
3    mike   3100
4    jack   3000
5    jack   5000
6    jack   3000
5    qack   5000
我希望的结果如下(以money字段为计数基础):
id   name  money  time  
1    jack   3000    1
2    tom    5000    1
3    mike   3100    1
4    jack   3000    2
5    jack   5000    2
6    jack   3000    2
5    qack   5000    3注:表中有几千条记录,要保证查询速度不受影响,各位有什么好建议:

解决方案 »

  1.   

    以money字段为计数基础,那爲什麽   6    jack   3000    2--->而不是3 阿???不太明白
      

  2.   

    1.在money字段建索引
    2.
    select a.name , a.money , b.times  from 
    (select name , money
    FROM table1
    ) a,
    (SELECT money,count(money) as times
    FROM table1
    group by money) b
    where a.money = b.money
      

  3.   

    不好意思,刚才用了copy,应该这样才对
    id   name  money  time  
    1    jack   3000    1
    2    tom    5000    1
    3    mike   3100    1
    4    jack   3000    2
    5    jack   5000    2
    6    jack   3000    2
    7    qack   5000    3
      

  4.   

    laughsmile的并没有符合搂主的意思啊
      

  5.   

    没看懂你的意思6 jack 3000为什么不是3?
      

  6.   

    shenjiong6() 
    以money字段为计数基础,而不是以name为基础
      

  7.   

    不好意思,shenjiong6() 说得对
    6 jack 3000 后面是3
      

  8.   

    1.假设id是自增长字段identity:
    select name, moneys,
    (select count(id) from table1 where   moneys=a.moneys and id<=a.id) as counts  
    from table1 a
    2.如果id不是自增长字段,则需要创建有自增长字段的临时表:
    select identity(int,1,1) as id,* into #temp 
    from tablename
    select name, moneys,
    (select count(id) from #temp where   moneys=a.moneys and id<=a.id) as counts  
    from #temp a
    drop table #temp 
      

  9.   

    select name, money,sum(count) as time
    from (select count(id) as count from table a, table b where   a.money=b.money)
    group by name,money 没试过,看看行否
      

  10.   

    在mysql中,好像不好解决。没有更好的办法么?