一、内置函数
1.字符串处理函数
--concat(字符串1,字符串2....):合并字符串
--trim(str):去掉所有空格
--ltrim(str):去掉左空格
--rtrim(str):去掉右空格
--substring(str,x,y):从x开始,连续取y个
2.数值处理函数
-- abs(x):返回绝对值
-- ceil(x):返回大于x的最小整数
-- floor(x):返回小于x的最大整数
-- mod(x,y):返回x与y的模
-- rand():返回[0-1)之间的随机数 select round(rand()*100);
-- round(x,y):返回参数x的y位小数的四舍五入结果
3.日期函数
--year(日期或日期时间类型字段):返回年份
--month(日期或日期时间类型字段):返回月份
--day(日期或日期时间类型字段):返回日期
二、聚合函数:
--count(*):返回记录数 只返回一条结果 不能跟普通字段一起查询,但可以配合聚合一起使用
--sum(数值类型的字段):求和
--max(数值类型的字段):求最大值
--min(数值类型的字段):求最小值
--avg(数值类型的字段):求平均值
三、关键字
-- distinct 去重 只能单独使用
SELECT distinct(nationality),name FROM student
-- 排序 order by 默认asc升序 desc降序
SELECT * FROM student WHERE sex='男' ORDER BY name asc,id desc
-- LIMIT 从0开始 查10个
SELECT * FROM tudent LIMIT 0,10
四、分组查询
关键字:group by 分组字段
having和where的区别:
1.where 在group by之前;having在group by之后
2.where是对原始数据的条件筛选;having是对统计后的数据的筛选条件
五、连接
-- 内连接 inner joio 为两张表的交集记录
select * from student inner join school on student.schid=school.id
-- 外连接
-- 左外连接 返回交集 然后返回左表剩余
select * from student left join school on student.schid=school.id
-- 右外连接 返回交集 然后返回右表剩余
select * from student right join school on student.schid=school.id
-- 全连接 左右连接返回字段数相对可以全连接
select * from student left join school on student.schid=school.id
union
select * from student right join school on student.schid=school.id
-- 自连接
select b.* from student as a
inner join student as b
on a.stuname='小明' and b.stuage>a.stuage
-- 嵌套链接
select * from student where age>(select age from student where name='小明')
六、编写顺序
少妇屋鬼混,哦了
select
from
where
group by
having
order by
limit
七、执行顺序
附近欧文肝癌还是斗殴了
from
on
join
where
group by
avg聚合函数
having
select
distinct
order by
limit