数据查询语言
1、查询表中某些字段
SELECT 字段1,字段2.。。 FROM 表名;
例:查询姓名与年龄
select name,age from Student;
* 在SQL 中也是通配符,代表所有字段
2、SQL中的数学运算 + - * /
例:查询每个教师的日薪,按每月30天计算
select name,salary/30 from teacher;
例:查询每个教师的年薪,按每年12月计算
select name,salary*12 from teacher;
3、字段别名
为查询结果新取一个字段名
例:查询每个教师的年薪,并为查询结果设置的字段名为yearsal
select name,salary*12 yearsal from teacher;
4、字符串操作
concat(s1,s2.。。);
返回连接参数产生的字符串,一个或多个待拼接的内容,任意一个为NULL则返回值为NULL
例:显示所有教师名和所教科目中间有下划线连接
select concat(name,'_',subject) from teacher;-----name_subject
concat_ws(x,s1,s2..);
返回多个字符串拼接之后的字符串,每个字符串之间有一个x
例:连接字符串,并使用空格分隔
select concat_ws(' ',name,subject,'he') from teacher;-------name subject he
substring(s,n,len) mid(s,n,len)
两个函数作用相同,从字符串s中返回一个第n字符开始、长度为len 的字符串
例:获取 名字 编号
select mid(name,1,3) from teacher;---------name=hehe --heh 从1开始
left(s,n) right(s,n)
前者返回字符串s从最左边开始的n个字符,后者返回字符串s从最右边开始的n个字符
例:获取 名字,不要编号
select left(name,3) from teacher; --name=hehe --heh
select right(name,3) from teacher; --name=hehe --ehe
insert(s1,x,len,s2)
返回字符串s1,其子字符串起始于位置x,被字符串s2取代s1的len个字符
select insert('12345678',3,4,'ABCDEF');
"12ABCDEF78"
replace(s,s1,s2)
返回一个字符串,用字符串s2替代字符串s中所有的字符串s1
select replace('12345678','345','ABC');
'12ABC678'
select replace('12345678345','345','ABC');
'12ABC678ABC'
locate(str1,str) position(str1 in str) instr(str,str1)
三个函数作用相同,返回子字符串str1在字符串str中的开始位置(从第几个字符开始)
select locate('123','ABCDD123DEF'); ---6
select position('123' in 'ABCDD123DEF');
select instr('ABCDD123DEF','123');
field(s,s1,s2,...) 返回第一个与字符串s匹配的字符串的位置
select field('123','ABC','DEF','123','HEHE');----3
重点:SQL语言中 下标从1开始
5、排重显示
select distinct 字段 from 表名;
查询:学校一共开设了多少科目
select distinct * from teacher;
name | sex | age | info |
+------+------+------+--------+
| hehe | m | 18 | sss |
| haha | f | 18 | aa
select distinct age from student;
+------+
| age |
+------+
| 18 |
+------+
6、条件查询
select 字段 from 表名 where 条件
当where条件为真,显示相关数据,配合相关比较运算符
> < >= <= = !=(<>)
例:查询所有教s1科目的老师
select * from teacher where subject = 's1';
例:查询所有不教s1科目的老师
select * from teacher where subject != 's1';
或 select * from teacher where not subject = 's1';
例:查询所有年薪超过8W的老师
select name,id,salary*12 from teacher where salary*12>=80000;
注意:SQL中字符串可以直接使用, 比较运算符 与strcmp比较规则一致。
select * from 表名 where x between n and m;
显示x值 在n到m之间的数据 (n ,m]
例:查询月薪在6667-6669之间的教师
select * from teacher where salary between 6667 and 6669;
或 select * from teacher where salary > 6667 and salary<= 6669;
select * from 表名 where x in(n1,n2,n3);
显示x值在(n1,n2,n3,。。。)列表中的相关数据
例:查询教授s1,s4,s5等科目的老师信息
select * from teacher where subject in ('s1','s4','s5');
select * from 表名 where x like str;
str中字符串可以进行模糊查询,str可以使用通配符
%:表示任意多个字符
_:代表一个字符
例:查询名字mage开头的相关老师信息
select * from teacher where name like 'mage%';
例:查询名字以1结尾的相关老师信息
select * from teacher where name like '%1';
select * from teacher where name like '%1_';
注意:在SQL中也可使用逻辑,and or not , && || !
7、空值处理
在SQL中空值是一种特殊数据,任何数据与空值进行计算结果为空
ifnull(x,n) 如果x的值为空,则用n来替换(MySQL,Oracle使用nvl函数)
例:查询员工表中的年薪
select name,base_sal*ifnull(royalty,1)*12 from Employee;
例:查询没有年终奖的员工
select * from Employee where royalty!=null; 不能用
select * from Employee where royalty is null;
例:查询有年终奖的员工
select * from Employee where royalty is not null;
8、排序(字符串也行 ASCII)
是对查询结果进行排序,而不是对数据库中表的数据进行排序
select 数据 from order by 字段 类型(升/降);
默认升序,DESC 降序(从大到小)
例:按基本工资进行排序
select * from Employee order by base_sal;
+------+
| age |
+------+
| NULL |
| 18 |
| 19 |
+------+
例:按年终奖系数进行排序 降
select * from Employee order by royalty desc;
+------+
| age |
+------+
| 19 |
| 18 |
| NULL |
+------+
NULL 升序 最小
降序 最小
例:基本工资为第一个排序字段,年终奖系数为第二排序字段
select * from Employee order by base_sal,royalty desc;
先按第一排序排序,当第一排序相同时,再按第二排序排序
9、单行函数
对表中的数据,处理一行就返回一个结果,这种函数叫单行函数
upper 小写转大写
select id,upper(name) from Employee;
lower 大写 转 小写
select id,lower(post) from Employee;
length 计算字符串长度
select id,length(name) from Employee;----name=hehe --长度4
10、组函数
一次查询只得到一个结果,如果在数据进行分组的情况下,一个分组得到一个结果
count 计数
例:统计101部门的员工数量
select count(id) from emp where dept_id=101;
例:统计学生数量
select count(name) from student; --4
max 求最大值 (也能算字符串 ASCII)
例:求基本工资最高值
select max(base_sal) from Employee;
min 求最小值
例:求基本工资最低值
select min(base_sal) from Employee;
sum 求和
例:求基本工资总支出
select sum(base_sal) from Employee;
avg 求平均
例:求员工平均工资
select avg(base_sal) from Employee;
11、多表查询
根据员工表,部门表查询出每个员工的上班地点
select emp.name,addr from emp,dept where dept_id=dept.id;
12、分组查询
select 组函数(字段) from 表名 group by 分组条件
例:每个部门的平均工资
select dept.name,avg(sal) from emp,dept where dept_id=dept.id group by dept_id;
来源:https://blog.csdn.net/weixin_42073412/article/details/100798614