mysql查询问题

syb1045 2010-02-01 12:27:27
bas_car 表 有列carname ,要进行数据统计,
select count(a.carname),a.carname from bas_car a,bas_car b where a.carid>=b.carid and a.carname=b.carname
group by a.carname
如上查询结果。 我现在要在结果中产生一列新序号列,如 1,2,3,4,5.
sqlserver中有row_number() over() 可以实现。
select count(a.carname),row_number() over(ordery by count(carname) ) as 序号,a.carname from bas_car a,bas_car b where a.carid>=b.carid and a.carname=b.carname group by a.carname 可以实现。
mysql怎么弄。帮帮忙,大家。 有点急。。
...全文
68 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2010-02-01
  • 打赏
  • 举报
回复
1、用变量实现,当然如果你用mysql5的话,可以写个函数实现

mysql> select * from t;
+------+
| s1 |
+------+
| 6 |
| 6 |
| 0 |
| 19 |
| 19 |
| 1 |
| 2 |
| 3 |
| 4 |
| 0 |
| 1 |
| 2 |
| 4 |
+------+
13 rows in set (0.44 sec)

mysql> set @rownum=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @rownum:=@rownum+1 as rownum,s1 from t;
+--------+------+
| rownum | s1 |
+--------+------+
| 1 | 6 |
| 2 | 6 |
| 3 | 0 |
| 4 | 19 |
| 5 | 19 |
| 6 | 1 |
| 7 | 2 |
| 8 | 3 |
| 9 | 4 |
| 10 | 0 |
| 11 | 1 |
| 12 | 2 |
| 13 | 4 |
+--------+------+
13 rows in set (0.04 sec)

2、mysql5中使用函数实现,原理和上面使用变量差不多

下面过程的目的是获得整型包含行的数量的结果集,类似其他DBMS中的ROWNUM()。我们需要一个用户变量来保存在每次调用rno()后的结果,就命名为@rno吧。

CREATE FUNCTION rno ()
RETURNS INT
BEGIN
SET @rno = @rno + 1;
RETURN @rno;
END;

通过rno()方法的SELECT我们获得了行数。下面是调用程序的结果:

mysql> SET @rno = 0;//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT rno(),s1,s2 FROM t;//
+-------+------+------+
| rno() | s1 | s2 |
+-------+------+------+
| 1 | 1 | a |
| 2 | 2 | b |
| 3 | 3 | c |
| 4 | 4 | d |
| 5 | 5 | e |
+-------+------+------+
5 rows in set (0.00 sec)

在SELECT中将@rno置零的技巧是使用了WHERE的求值功能,而这个特性在今后的MySQL中可能丢失。

CREATE FUNCTION rno_reset ()
RETURNS INTEGER
BEGIN
SET @rno = 0;
RETURN 1;
END;
SELECT rno(),s1,s2 FROM t WHERE rno_reset()=1;//


实例如下:
mysql> use db5
Database changed
mysql> delimiter //
mysql> CREATE FUNCTION rno ()
-> RETURNS INT
-> BEGIN
-> SET @rno = @rno + 1;
-> RETURN @rno;
-> END;//
Query OK, 0 rows affected (0.42 sec)

mysql> set @rno = 0;
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> select rno(),s1 from t;//
+-------+------+
| rno() | s1 |
+-------+------+
| 1 | 6 |
| 2 | 6 |
| 3 | 0 |
| 4 | 19 |
| 5 | 19 |
| 6 | 1 |
| 7 | 2 |
| 8 | 3 |
| 9 | 4 |
| 10 | 0 |
| 11 | 1 |
| 12 | 2 |
| 13 | 4 |
+-------+------+
13 rows in set (0.06 sec)

mysql> CREATE FUNCTION rno_reset ()
-> RETURNS INTEGER
-> BEGIN
-> SET @rno = 0;
-> RETURN 1;
-> END;//
Query OK, 0 rows affected (0.01 sec)

mysql> select rno(),s1 from t where rno_reset()=1;//
+-------+------+
| rno() | s1 |
+-------+------+
| 1 | 6 |
| 2 | 6 |
| 3 | 0 |
| 4 | 19 |
| 5 | 19 |
| 6 | 1 |
| 7 | 2 |
| 8 | 3 |
| 9 | 4 |
| 10 | 0 |
| 11 | 1 |
| 12 | 2 |
| 13 | 4 |
+-------+------+
13 rows in set (0.13 sec)
叶子 2010-02-01
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20051017/13/4331208.html
yananguo_1985 2010-02-01
  • 打赏
  • 举报
回复

mysql> select a.carname,
-> (select count(*) from bas_car where col<=a.carid and carname=a.carname) as rownum
-> from bas_car a;

--小F-- 2010-02-01
  • 打赏
  • 举报
回复
学习
syb1045 2010-02-01
  • 打赏
  • 举报
回复
能不能直接象sql中一样。 用个sql语句就查询出来的?
我是新人,对这个函数之类的不是太理解。
不怎么会用

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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