DQL:数据查询语言 select
一、基础查询:
select 字段名 from 表名;
distinct :去重
as :起别名,起别名也可以用空格替代as
二、条件查询:where
< \> \<> \<= \>= \!= \= \between...and
or \and
in \not in
is null \is not null
select * from 表名 where 条件;
三、模糊查询:like
'%' 匹配多个字符, '_' 匹配单个字符。
select * from 表名 where (XXX like '%x%');
四、排序:order by 字段名 [desc | asc]
select * from 表名 where 条件 order by 字段名;
五、限制:limit 偏移量 条目数
select * from 表名 limit 偏移量,条目数;
六、聚合函数:sum min max avg count
select sum(字段) from 表名 where 条件;
select count(字段) from 表名 where 条件;
select max(字段) form 表名 where 条件;
七、分组查询:group by 字段名 having 分组后的基础上进行查询
select 字段,聚合函数 from 表名 where 条件 group by 字段 having 子查询
八、子查询:
select * from 表名 where 条件一 and 子查询条件
九、关联查询:
1、左关联:left join ... on
2、右关联:right join ... on
3、自关联:inner join ... on
select * from 表1 left join 表2 on 防止冗余采用等值操作 here 条件
十、系统函数:
1、字符串函数:
char_length(str):获取字符串的个数
length(str):获取字符串的字节数
concat(s1,s2...):连接s1,s2..为一个字符串
lower(str):将字符串str中所有的字符转换为小写
upper(str):将字符串str中所有的字符转换为大写
left(str,x):返回字符串str最左边的x个字符
right(str,y):返回字符串str最右边的y个字符
repeat(str,x):返回str中重复出现x次的结果
replace(str,a,b):将字符串str中的a更换为b
2、时间函数:
curdate():得到当前日期
curtime():得到当前时间
now():得到当前日期和时间
year(date):得到date的年份
month(date):得到date的月份
day(date):得到date的天
hour(time):得到time的小时
minute(time):得到time的分钟
second(time):得到time的秒
week(date):得到date是一年中的第几周
date_format(date,fmt):按格式化串fmt返回date的日期字符串
DATE_ADD(date,INTERVAL number unit)
date_sub():时间的加减
datediff(date1,date2):时间差
3、属性函数:
abs(x):求x的绝对值
cell(x):向上取整
floor(x):向下取整
round(x,d):四舍五入,d为保留小数的位数