Mysql中id自增的基础语句

  1、 要实现id的自增,要保证id是主键、int型、当然主键肯定不能为空。关键 字是auto_increment。

那么就先建一个table来看一下:

    create table user(id int not null primary key auto_increment,name  character(255),password character(255),email character(256));

 

如此就可以完成id自增的一个数据库表了。要注意,character中的最大值是255。

2、数据库查询学生成绩按排名排列:

Mysql中id自增的基础语句_第1张图片

基本数据时这样的,最终的查询结果是如下:

Mysql中id自增的基础语句_第2张图片

查询语句是:

select * from (select a.name,a.age,a.score,(select count(id)+1 from stude
nt where score >a.score) as mingci from student a) b where b.mingci <= 10 order
by b.mingci;

你可能感兴趣的:(Mysql中id自增的基础语句)