一、SQL语法——8-单表查询
8-单表查询
1.select语句的功能就是查询数据库,select语句的语法格式如下:
select column1,column2...
from 数据源
[where condition]
其中,数据源可以是表、视图等。where条件语句确定选择那些记录,只有满足where条件的行才会被选择出来,如果没有条件,则默认选出所有行,如果想选择出所有列,可以使用*代替column。
2.当使用select进行查询时还可以在select语句中使用算数运算符(+ - * /),从而形成算数表达式:
(1)对数值型数据列、变量、常量可以使用运算符(+ - * /)创建表达式;
(2)对日期数据列、变量、常量可以使用部分运算符(+ -)创建表达式,两个日期之间可以进行减法运算,日期和数值之间可以进行加减运算;
(3)运算符不仅可以在列和常量、变量之间进行运算,也可以在两列之间进行运算。
3.单表查询示例:
#创建两个表teacher_table以及student_table create table teacher_table( teacher_id int auto_increment, teacher_name varchar(255), primary key(teacher_id) ); create table student_table( student_id int auto_increment primary key, student_name varchar(255), java_teacher int, foreign key (java_teacher) references teacher_table(teacher_id) ); #为表中添加数据 insert into teacher_table values (null,'teacher_1'), (null,'teacher_2'), (null,'teacher_3'); insert into student_table values (null,'studnet_1',1), (null,'studnet_2',1), (null,'studnet_3',1), (null,'studnet_4',2), (null,'studnet_5',2), (null,null,2), (null,'studnet_6',null); #选择出teacher_table表中的所有行、所有列的数据 select * from teacher_table; #增加where条件则只选出满足where条件的记录 select student_name from student_table where java_teacher > 3; #使用算数运算符 #数据列可以当成一个变量 select teacher_id + 5 from teacher_table; #select后不仅仅可以是数据列,也可以是表达式,还可以是变量、常量等 select 3*5,20 from teacher_table; #查询出teacher_table表中teacher_id * 3 > 4的记录 select * from teacher_table where teacher_id * 3 > 4; #MySQL使用concat函数进行字符串连接运算 #选出teacher_name和'xx'字符串连接后的结果 select concat(teacher_name,'xx') from teacher_table; #对于MySQL而言,如果在算数表达式中使用null,将会导致整个算数表达式的返回值为null; #如果在字符串连接运算中出现null,将会导致连接后的结果也是null select concat(teacher_name,null) from teacher_table; #为数据列或者表达式起别名时,别名紧跟数据列,中间以空格隔开,或者使用as关键字隔开 select teacher_id + 5 as MY_ID from teacher_table; #为列起别名可以改变列的标题头,用于表示计算结果的具体含义。如果列别名中使用特殊字符(例如空格), #或者需要强制大小写敏感,都可以是通过为别名添加双引号来实现 select teacher_id+5 "MY'id" from teacher_table;
#如果选择多列,并为多列起别名,则列与列之间用逗号隔开,列与别名之间用空格隔开 select teacher_id+5 MY_ID,teacher_name 老师名 from teacher_table; #不仅可以为列其别名,也可以为表起别名 select teacher_id+5 MY_ID,teacher_name 教师名 from teacher_table t; #列名可以当成变量处理,所以运算符也可以在多列之间进行运算 select teacher_id+5 MY_ID,concat(teacher_name,teacher_id) teacher_name from teacher_table where teacher_id*2>3; #==================================================================================== #运算符 #distinct #select默认把所有符合条件的记录选择出来,即使这些记录完全一样 #如果想要去除重复行,可以使用distinct关键字从查询结果中清除重复行 #选出所有记录,包括重复行 select student_name,java_teacher from student_table; #去除重复行 select distinct student_name,java_teacher from student_table; #between and #选出student_id大于2小于4的全部记录 select * from student_table where student_id between 2 and 4; #选出student_table中java_teacher小于等于2,student_id大于等于2的记录 select * from student_table where 2 between java_teacher and student_id; #in #选出student_id为2或者4的记录 select * from student_table where student_id in(2,4); #选出student_id,java_teacher列值为2的所有记录 select * from student_table where 2 in (stduent_id,java_teacher); #like #like用于进行模糊查询 #SQL中的两个通配符 #_:代表一个任意字符 #%:代表多个任意字符 #查出所有学生名字中以stduent开头的学生 select * from student_table where student_name like 'student%'; #查出所有学生中名字为两个字符的学生 select * from student_table where student_name like '__'; #MySQL使用转义字符\可以使得_或者%称为一个普通的字符 #查询出所有以_开头为名字的学生 select * from student_table where student_name like '\_%'; #标准的SQL语句使用转义字符必须使用escape来定义转义字符 #查询出所有以_开头为名字的学生(SQL标准语言) select * from student_table where student_name like '\_%' escape '\'; #order by子句 #order by子句用于排列查询结果的顺序 #选出student_table表中的所有记录,按照java_teacher列的升序排列 select * from student_table order by java_teacher; #先按照java_teacher列的降序排列,当java_teacher列相同时,按照student_name列的升序排列 select * from stuedent_table order by java_teacher desc,student_name;