select语句使用详解
select语句是基础操作中比较复杂的部分,我们单拿出来详细解析一下。还是以上一篇文章里的student表为例。
select * from student:查询student表中所有记录。
create table stu2 select * from student:创建一个stu2表,内容与student是一样的,但注意,主键值是不一样的。
因此我们就可以使用alter table stu2 modify id int not null auto_increment primary key这个SQL语句来重新定义主键'id'。
投影和选择:
投影:select name,age from stu2。选择某几列作为查询内容,这叫做投影。
select name as 姓名,age as 年龄 from stu2:对列投影进行重命名。
选择:select * from stu2 where id>3。选择某几行作为查询内容,这叫做选择。
DISTINCT:去掉重复行。
select DISTINCT name,age,sex from stu2:去掉重复的name,age,sex段数据。
条件。select * from stu2 where name='张三' and id>3:where的选择。and表示与,也可以用or表示或。
select * from stu2 where id in(3,5,6):表明显示id为3,5,6的三行数据。
select * from stu2 where id in(select id from stu