日期:2014-05-16  浏览次数:21013 次

select时添加自增的编号(就像oracle中的rowid,rownum) 急!急!急!
SQL code

mysql>      SELECT  mobile_num,COUNT(DISTINCT vlr) AS vlrcount FROM alarmdetail
GROUP BY mobile_num ORDER BY vlrcount DESC LIMIT  10;
+---------------+----------+
| mobile_num    | vlrcount |
+---------------+----------+
| 8615868157030 |        4 |
| 8615888432117 |        4 |
| 8615888757011 |        3 |
| 8615824157681 |        3 |
| 8615888779740 |        3 |
| 8613806853520 |        3 |
| 8615824459241 |        3 |
| 8613566124713 |        3 |
| 8615888202117 |        3 |
| 8613566169670 |        3 |
+---------------+----------+
10 rows in set (0.00 sec)


我想让查询的结果前面有1,2,3,4,...,10这样的编号。这个该怎么搞啊
请大家帮帮忙啊。

在csdn上找到这样的方法,在我的这个结果中不行啊。
SQL code

mysql>      SELECT (SELECT   COUNT(1)+1   FROM   alarmdetail   b   WHERE   b.id
alarmdetail.id   ) AS Rank, mobile_num,COUNT(DISTINCT vlr) AS vlrcount
    ->      FROM alarmdetail
    ->      GROUP BY mobile_num
    ->      ORDER BY vlrcount DESC LIMIT 10;
+------+---------------+----------+
| Rank | mobile_num    | vlrcount |
+------+---------------+----------+
|  132 | 8615868157030 |        4 |
|  278 | 8615888432117 |        4 |
|  264 | 8613566233443 |        3 |
| 1147 | 8613567771731 |        3 |
|  259 | 8613806853520 |        3 |
| 1076 | 8615824145234 |        3 |
|  142 | 8615858155622 |        3 |
| 1384 | 8615868418510 |        3 |
|  361 | 8615869009887 |        3 |
|  337 | 8615869174411 |        3 |
+------+---------------+----------+
10 rows in set (0.48 sec)




------解决方案--------------------
SELECT mobile_num,COUNT(DISTINCT vlr) AS vlrcount FROM alarmdetail
GROUP BY mobile_num ORDER BY vlrcount DESC LIMIT 10;

存为VIEW NEWTT:
SELECT A.mobile,A.vlrcount,COUNT(B.vlrcount) FROM NEWTT A INNER JOIN NEWTT B
ON A.mobile>=B.mobile GROUP BY A.mobile,A.vlrcount

------解决方案--------------------
2楼3楼都是不错的方法。
------解决方案--------------------
参考下贴中的多种方法。

http://blog.csdn.net/ACMAIN_CHM/archive/2009/04/20/4095531.aspx
MySQL中的ROWNUM的实现
------解决方案--------------------
探讨
按mobile_num排序?结果怎么不对?详细说明一下