135-1821-9792

mysql序号怎么弄,mysql自动生成序号

mysql分组 排序 加 序号

需要用row_number来给分组添加序号。

创新互联专注于企业成都全网营销、网站重做改版、金东网站定制设计、自适应品牌网站建设、H5高端网站建设商城网站建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为金东等各大城市提供网站开发制作服务。

1、创建测试表,插入数据:

create table test(sid int,sname varchar(20),sclass varchar(20),score int); insert into test values (1,'张三','一年一班',100)insert into test values (2,'李四','一年一班',78)insert into test values (3,'王五','一年一班',67)insert into test values (4,'赵六','一年一班',87)insert into test values (5,'badkano','一年二班',98)insert into test values (6,'百度知道团长','一年二班',99)insert into test values (7,'du小小动','一年二班',99)insert into test values (8,'刘备','一年三班',56)insert into test values (9,'张飞','一年三班',67)insert into test values (10,'关羽','一年三班',76)

2、要求按照班级总分给出班级排名(即序号),执行语句:

1

select row_number() over (order by score desc) 排名,sclass 班级,score 总分 from (select sclass,SUM(score) score from test group by sclass) t

3、查询结果:

mysql 怎么才能做到rownumber序号

1. 直接在程序中实现;

这应该算是效率最高的一种,也极为方便。直接在你的开发程序中(PHP/ASP/C/...)等中,直接初始化一个变量nRowNum=0,然后在while 记录集时,nRowNum++; 然后输出即可。

2. 使用MySQL变量;在某些情况下,无法通过修改程序来实现时,可以考虑这种方法。

缺点,@x 变量是 connection 级的,再次查询的时候需要初始化。一般来说PHP等B/S应用没有这个问题。但C/S如果connection一只保持则要考虑 set @x=0

mysql select @x:=ifnull(@x,0)+1 as rownum,id,col

- from tbl

- order by col;

+--------+----+------+

| rownum | id | col |

+--------+----+------+

| 1 | 1 | 26 |

| 1 | 3 | 35 |

| 1 | 2 | 46 |

| 1 | 4 | 68 |

| 1 | 6 | 92 |

| 1 | 5 | 93 |

+--------+----+------+

6 rows in set (0.00 sec)

3. 使用联接查询(笛卡尔积)

缺点,显然效率会差一些。

利用表的自联接,代码如下,你可以直接试一下 select a.*,b.* from tbl a,tbl b where a.col=b.col 以理解这个方法原理。

mysql select a.id,a.col,count(*) as rownum

- from tbl a,tbl b

- where a.col=b.col

- group by a.id,a.col;

+----+------+--------+

| id | col | rownum |

+----+------+--------+

| 1 | 26 | 1 |

| 2 | 46 | 3 |

| 3 | 35 | 2 |

| 4 | 68 | 4 |

| 5 | 93 | 6 |

| 6 | 92 | 5 |

+----+------+--------+

6 rows in set (0.00 sec)

4. 子查询

缺点,和联接查询一样,具体的效率要看索引的配置和MySQL的优化结果。

mysql select a.*,

- (select count(*) from tbl where col=a.col) as rownum

- from tbl a;

+----+------+--------+

| id | col | rownum |

+----+------+--------+

| 1 | 26 | 1 |

| 2 | 46 | 3 |

| 3 | 35 | 2 |

| 4 | 68 | 4 |

| 5 | 93 | 6 |

| 6 | 92 | 5 |

+----+------+--------+

6 rows in set (0.06 sec)

做为一款开源的数据库系统,MySQL无疑是一个不做的产品。它的更新速度,文档维护都不逊于几大商业数据库产品。估计在下一个版本中,我们可以看到由MySQL自身实现的ROWNUM。

mysql中如何设置一个字段,表中每增加一个值,这个字段自动产生一个连续的序号?

通过 AUTO_INCREMENT设置

SQL INSERT语句的时候,要避免 指定那个自增的字段.否则会发生主键的冲突。

通过 ALTER TABLE语句 可以修改 自增的数值, 但是只能增加,不能减少。

TRUNCATE TABLE 语句,会将自增ID重置为零。

mysql CREATE TABLE test_create_tab2 (

-   id   INT  AUTO_INCREMENT,

-   val  VARCHAR(10),

-   PRIMARY KEY (id)

- );

Query OK, 0 rows affected (0.09 sec)

mysql INSERT INTO test_create_tab2(val) VALUES ('NO id');

Query OK, 1 row affected (0.03 sec)

mysql select last_insert_id() as id;

+----+

| id |

+----+

|  1 |

+----+

1 row in set (0.00 sec)

mysql INSERT INTO test_create_tab2(val) VALUES ('NO id 2');

Query OK, 1 row affected (0.03 sec)

mysql select last_insert_id() as id;

+----+

| id |

+----+

|  2 |

+----+

1 row in set (0.00 sec)

mysql select * from test_create_tab2;

+----+---------+

| id | val     |

+----+---------+

|  1 | NO id   |

|  2 | NO id 2 |

+----+---------+

2 rows in set (0.00 sec)

mysql 设置序号列

alter table abc change num num int primary key auto_increment

前提是你的表中不能存在主键,否则请使用:

alter table abc change num num int auto_increment

一般情况下增长都是主键

请教如何取得mysql 排序的序号

mysql select * from a;

+-----+

| col |

+-----+

| 0 |

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

| 6 |

| 7 |

+-----+

8 rows in set (0.00 sec)

mysql set @i := 0; select @i := @i + 1 as `order`, a.* from a order by col desc;

+-------+-----+

| order | col |

+-------+-----+

| 1 | 7 |

| 2 | 6 |

| 3 | 5 |

| 4 | 4 |

| 5 | 3 |

| 6 | 2 |

| 7 | 1 |

| 8 | 0 |

+-------+-----+

8 rows in set (0.00 sec)

mysql

mySQL数据库中ID如何自动编号?如图所示 选哪个设置?新手求带,谢谢!

如果有

phpmyadmin

的话,可以直接在建表的时候,有个“额外”这个选项,点击进入,选中AUTO_INCREMENT

如果不能的话,则用php页面创建表的时候,在

sql语句

插入$sql="create

table

$table_name(id

varchar(4)

not

null

primary

key

auto_increment";再执行就可以了

需要说明的是,这个字段不是必须填入的~


文章标题:mysql序号怎么弄,mysql自动生成序号
转载源于:http://kswsj.com/article/hcjgoc.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号