1.mysql 关键字
关键字 | 说明 | 示例 |
---|---|---|
DISTINCT | 指示mysql只返回不同的值 | select distinct vend_id from products; |
limit | 限制结果 | select prod_name from products limit 5; |
DESC | 限定为降序排序 | select prod_name from products order by prod_name desc; |
ASC | 升序排序,系统默认 | select prod_name from products order by prod_name; |
like | 表示后面语句为一个pattern | select prod_id from products where prod_name like ‘jet%’; |
regexp | 表示后面为正则表达式 | select prod_id from products where prod_name regexp ‘.000’ |
binary | 指示区分大小写 | …where prod_name regexp binary ‘JetPack .000’ |
2.limit,限制结果
mysql> select prod_name from products limit 5;
指示返回不多于5行结果,从第一行开始
mysql> select prod_name from products limit 5,3;
指示返回不多于3行结果,从行5开始
注:mysql中的table从行0开始,即第一行为行0
和第二个limit语法作用相同的语句:mysql> select prod_name from products limit 3 offset 5;
指示返回不多于3行结果,从行5开始
3.mysql支持的条件运算符
条件运算符 | 说明 |
---|---|
<> | 不等于 |
!= | 不等于 |
BETWEEN | 在指定的两个值之间 |
NULL | 空值 |
is null | 判断是否为空 |
4.mysql操作符:用来连接和改变where子句中的子句的关键字,也称为逻辑操作符
操作符 | 说明 | 示例 |
---|---|---|
and | 同C语言&& | select prod_name from products where vend_id = 1003 and prod_price >=10; |
or | 同C语言|| | select prod_name from products where vend_id = 1003 or vend_id = 1002; |
in | 和or的作用相同 | select prod_name from products where vend_id in(1002,1003); |
not | 同C语言!,取反 | select prod_name from products where vend_id not in(1002,1003); |
5.通配符,like打头表示为通配pattern
wildcard | 说明 | 示例 |
---|---|---|
% | 匹配任意字符出现任意次数 | select prod_id,prod_name from products where prod_name like ‘jet%’; |
_ | 只能匹配单个字符 | select prod_id,prod_name from products where prod_name like ‘_ ton anvil’; |
注意:通配符%可以匹配任何字符,但不能匹配NULL
6.正则表达式,由关键字regexp起头表示
特殊字符 | 说明 | 示例 |
---|---|---|
. | 匹配任意一个字符 | …where prod_name regexp ‘.000’; |
| | 正则表达式中的或操作 | …where prod_name regexp ‘1000|2000’; |
\\ | mysql的转义字符 | …where vend_name regexp ‘\.’; |
^,$ | 位置锚定 | —— |
7.创建计算字段
concat函数:拼接字段;例:
select concat(rtrim(vend_name),' (',rtrim(vend_country),')') as vendor_title from vendors;
8.数据处理函数
[1]文本处理函数
函数 | 说明 | 函数 | 说明 |
---|---|---|---|
left() | 返回串左边的字符 | right() | 返回串右边的字符 |
length() | 返回串的长度 | locate() | 找出串的一个子串 |
lower() | 将串转换为小写 | upper() | 将串转换为大写 |
Ltrim() | 去丢串左边的空格 | Rtrim() | 去丢串右边的空格 |
soundex() | 返回串的soundex值 | substring() | 返回子串的字符 |
[2]日期和时间处理函数
函数 | 说明 | 函数 | 说明 |
---|---|---|---|
date() | 返回日期时间的日期部分 | time() | 返回日期时间的时间部分 |
hour() | 返回时间小时部分 | minute(),second() | 返回时间分钟,秒部分 |
year() | 返回日期的年份部分 | month(),day() | 返回日期月份,天数部分 |
now() | 返回当前日期和时间 | dayofweek() | 给定日期返回星期几 |
[3]数值处理函数
函数 | 说明 | 函数 | 说明 | 函数 | 说明 |
---|---|---|---|---|---|
abs() | 返回绝对值 | rand() | 返回一个随机数 | tan() | 返回角度的正切 |
exp() | 返回一个数的指数 | pi() | 返回圆周率 | sqrt() | 返回平方根 |
cos() | 返回一个角度的余弦 | sin() | 返回一个角度的正弦 | mod() | 返回除操作的余数 |
9.汇总数据函数
函数 | 说明 | 示例 |
---|---|---|
avg(列名) | 返回列的均值 | select avg(prod_price) as avg_price from products; |
count(*) | 返回这个表的行数 | select count(*) as num_prod from products; |
count(列名) | 得该列行数,不含NULL | select count(vend_id) as num_id from products; |
max(列名) | 返回某列最大值 | select max(vend_id) as max_id from products; |
min(列名) | 返回某列最小值 | select min(vend_id) as min_id from products; |
sum(列名) | 返回某列值之和 | select sum(vend_id) as sum_id from products; |
注意:在对列操作的聚集函数中可以使用distinct关键字,限定操作对象
10.数据分组
group by创建分组,然后对每个组的结果进行聚集。
select vend_id,count(*) as num_prods from products group by vend_id;
having过滤分组,示例如下:
select vend_id,count(*) as num_prods from products group by vend_id having count(*)>=3;
select 子句顺序
SELECT–>FROM–>WHERE–>GROUP BY–>HAVING–>ORDER BY–>LIMIT