select score,count(*) as '人数' ,(select count(*) from tb   b where  score <=b.score) as  '排名'
from tb 
group by score 
order by score desc
+-------+------+------+
| score | 人数 | 排名 |
+-------+------+------+
|    99 |    1 |    9 |
|    98 |    2 |    9 |
|    96 |    2 |    9 |
|    93 |    2 |    9 |
|    91 |    1 |    9 |
|    90 |    1 |    9 |mysql> select score,count(*) as '人数' ,(select count(*) from tb   where a.score
 <=score) as  '排名'
    -> from tb a
    -> group by score
    -> order by score desc;
+-------+------+------+
| score | 人数 | 排名 |
+-------+------+------+
|    99 |    1 |    1 |
|    98 |    2 |    3 |
|    96 |    2 |    5 |
|    93 |    2 |    7 |
|    91 |    1 |    8 |
|    90 |    1 |    9 |
+-------+------+------+
6 rows in set (0.02 sec)
另外select distinct 组团编号,route.线路编号,名称,导游编号,count(*)  as '总数',生效时间,team.状态
from team,compact,route
where compact.线路编号=route.线路编号
and compact.合同编号=team.合同编号
group by  组团编号
这个查询count(*)问什么不报错,不是分组后select的列要么是分组列要么是用于聚集函数么??? 
select distinct 组团编号,线路编号,导游编号,生效时间,状态,( select count(*)  from team b where b.组团编号=组团编号) as '团人数'
from compact,team
where compact.合同编号=team.合同编号
我是这样写的,所以不太明白count(*)问什么不报错??? 
想问一下高手,这是什么问题,期待回答

解决方案 »

  1.   

    select score,count(*) as '人数' ,
    (select count(*) from tb   b where  score  <=b.score) as  '排名'
    from tb 
    group by score 
    order by score desc先来分析这条语句。当MySQL看到这个子查询时 select count(*) from tb   b where  score  <=b.score 会怎么解释 score  <=b.score?score  当然是指的本表,b.score 指的也是本表。
      

  2.   

    select distinct 组团编号,route.线路编号,名称,导游编号,count(*)  as '总数',生效时间,team.状态
    from team,compact,route
    where compact.线路编号=route.线路编号
    and compact.合同编号=team.合同编号
    group by  组团编号
    这个查询count(*)问什么不报错,不是分组后select的列要么是分组列要么是用于聚集函数么???MySQL支持非标准SQL语句(虽然我本人对此不赞成)
    允许在GROUP查询时不列出所有非聚合列名。

    1.8.4. MySQL对标准SQL的扩展不需要在GROUP BY部分命名所有选择的列。对于某些十分特殊但相当正常的查询,它能提供更好的性能。请参见12.10节,“与GROUP BY子句同时使用的函数和修改程序”。o        可以与GROUP BY一起指定ASC和DESC。
    
      

  3.   

    select count(*) from tb  b where  score <=b.score
    那为什么只有一个数字9呢???而不是别的数字,为什么选的第一行的结果作为count(*)的结果??
    那么第二个问题是什么原因??我刚才回复了你在
    windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes 帖子的回答
    你的答案是正确的。还想请教你另一个帖子:
    感觉怪异的一个问题(mysql),高手进 
    十分感谢你的回答。。
      

  4.   

    select count(*) from tb  b where  score  <=b.score这句话等同于select count(*) from tb  b where  b.score  <=b.score
      

  5.   

    第二个问题是什么原因原因是 1.8.4. MySQL对标准SQL的扩展
      

  6.   

    Binlogging on server not active 这是什么错误????这不是错误,是你没有打开二进制日志功能。
      

  7.   

    想问一下,能不能回一下帖子
    感觉怪异的一个问题(mysql),高手进
    我准备把这个帖子结了