条件运算符:> < = <> <= >=;
不等可以用!=表示,也可以用<>表示,mysql推荐后者;
逻辑运算符:and or not;

模糊查询:

like关键字:

通配符%:表示任意个字符
通配符_:表示单个字符
可以是‘’进行转义,和java一样。也可以自定义转义字符。如:name like 'a_$\_',escape '$' 即将'$'定义为转义字符

between....and...(包含临界值的)

in关键字

可以用来代替or
select * from employees where name='kobe'or name='james'or name='yaoming';
可以写成select * from employees where name in('kobe','james','yaoming');简化写法

is null关键字

select * from employees where age=null,这种写法是错误的,‘=’不能判断null,只能写成where age is null
同理:is not null对应相同的用法

<=>安全等于

可以判断null,也可以判断普通值

排序查询 order by

默认升序 asc可以不写,降序使用desc

常用函数

单行函数

字符函数

1.length(str),获取字符的字节数,(utf-8编码下一个中文字符占三个字节)

2.concat(str1,str2,str3)拼接字符串,通过str2连接左右字符串

3.upper(str) lower(str)字符大小写转换

4.substr(str,begin,个数)或substr(str,begin)截取字符串。 mysql中索引从1开始

5.instr(str,substr)返回子串第一次出现的索引,如不匹配,则返回0;

6.trim(' str ')去除字符串前后的空格,还可以去除前后指定的字符trim('a' from 'aaaaahelloaaaaaa'),即除去a;

7.lpad('张三',5,'_')用指定的字符“_”去左填充字符串至指定的长度,即‘_*_张三’,同理 rpad为右填充

8.replace()指定字符替换,eg:select("张无忌爱上了周芷若","周芷若","赵敏")
输出:张无忌爱上了赵敏

数字函数

1.round,四舍五入,eg:select round(1.45),输出:1;eg:select round(1.456,2),即四舍五入后小数点保留2位,输出:1.47;
2.ceil 向上取整

3.floor 向下取整

4.truncate 截断,eg:select truncate(1.69999,1),output:1.6,不会四舍五入,直接截断

5.mod 趋于 eg:select mod(10,3) output:1;

日期函数

1.now 返回当前系统日期和事件 eg:select now();
2.curtime 返回当前时间,不包含日期 eg:select curtime();
3.datediff 获取两个日期之差 eg:select datediff('2019-7-13','1998-7-16');output:7667,输出为天数

其它函数
流程控制函数

1.if函数
select if(10>7,"true",“false”); output:true;

2.case函数。

等值判断

case 表达式
when 值1 then 结果1
when 值2 then 结果2
else 结果n
end

区间判断;
case
when 条件1 then 结果1
when 条件2 then 结果2
else 结果3
end

分组函数,也叫聚合函数,统计函数,用于统计使用

sum()求和
avg()求平均
count()计算非空字段的个数;count(*):统计结果集行数,常搭配distinct去重
max()
min()

分组查询:
group by

image.png


Scavenger
12 声望1 粉丝

« 上一篇
flex布局