select语句
查询数据
语法格式:
select 去重 要查询的字段 from 表名
xxx join 要连接的表
on 等值判断
where 过滤
group by 根据哪个字段分组
having 过滤分组数据
order by (asc|desc)排序
limit 起始下标 显示数据的长度
查询所有数据
select * fromstudent
;
查询指定字段
selectid
fromstudent
;
查询多个字段,用逗号隔开
selectid
,name
fromstudent
;
给查询到的数据字段取别名(也可以给别名),使用AS
selectid
as 学生学号 fromstudent
;
Concat(a,b)函数(连接)
select concat(‘学生学号:’,id
) as 学号 fromstudent
;
计算
select 100*2+62 as 计算结果;
查询自增增量
select @auto_increment_increment;
查询系统版本
select version();
假如我们有一些学生的分数,给所有学生的分数都+1
selectstudentnum
+ 1 as ‘加分后’ fromstudent
;
如果想要筛选查询的结果,只需要在后面写一个where就好了
selectstudentnum
fromstudent
wherestudentnum
between1002 and1003;
去重
使用distinct
语法格式:
select distinct 字段[] from 表名
查询并去重
select distinctstudentnum
from student;
数据库中的表达式:文本值、列、NULL、函数、计算表达式、系统变量…
模糊查询
下面具体的条件可以参考我写的运算符那一篇文章
使用like运算符 %表示0到n个字符 _表示一个字符
查询名字中姓刘的
selectstudentname
fromstudent
wherestudentname
like ‘刘%’;
查询名字中姓刘并且名字只有两个字的
selectstudentname
fromstudent
wherestudentname
like ‘刘_’;
查询名字中带有佳的
selectstudentname
fromstudent
wherestudentname
like ‘%佳%’;
查找studentnum=1001、1002、1003的学生
selectstudentnum
fromstudent
wherestudentnum
in (1001,1002,1003);
查找电话为空的学生
selectstudentnum
,studentname
fromstudent
wherephone
=’’ orphone
is null;