Mysql高阶语句(一)
- 一、MySQL高级进阶SQL 语句
- 1、SELECT斜体样式
- 2、DISTINCT
- 3、WHERE
- 4、AND、OR
- 5、IN
- 6、BETWEEN
- 7、通配符、LIKE
- 8、ORDER BY
- 9、| | 连接符
- 10、GROUP BY
- 11、HAVING
- 二、函数
- 1、数学函数
- 2、聚合函数
- 3、字符串函数
- 4、日期时间函数
一、MySQL高级进阶SQL 语句
1、SELECT斜体样式
- 显示表格中一个或数个字段的所有资料
- 语法:SELECT 字段 FROM 表名
select region from xc;

2、DISTINCT
- 不显示重复的资料(去重)
- 语法:SELECT DISTINCT 字段 FROM 表名
select distinct region from xc;

3、WHERE
- 有条件查询
- 语法:SELECT 字段 FROM 表名 WHERE 条件
select store_name from store where sales>1000;
select store_name from store where sales > 1000 or (sales<500 and sales>200);
4、AND、OR
- and(并且)、or(或者)
- 语法:SELECT 字段 FROM 表名 WHERE 条件1 ([AND|OR] 条件2)+;
select store_name from store where sales>1000;
select store_name from store where sales > 1000 or (sales<500 and sales>200);


5、IN
- 显示已知的值的资料
- 语法:SELECT 字段 FROM 表名 WHERE 字段 IN (‘值1’,‘值2’,……);
select * from store where store_name in ('Los Angeles','Houston');

6、BETWEEN
- 显示两个值范围内的资料
- 语法:SELECT 字段 FROM 表名 WHERE 字段 BETWEEN ‘值一’ and ‘值二’;
select * from store where date between '2020-12-06' and '20200-12-10';

7、通配符、LIKE
- 通常通配符都是跟LIKE一起使用
| % | 百分号表示零个、一个或多个字符 |
|---|---|
| _ | 下划线表示单个字符 |
- LIKE:用于匹配模式来查找资料
- 语法:SELECT 字段 FROM 表名 WHERE 字段 LIKE ‘模式’;
select * from store where store_name like '%os%';

8、ORDER BY
- 按关键字排序
- 语法:SELECT 字段 FROM 表名 [WHERE 条件] ORDER BY 字段 [ASC,DESC];
#ASC 按照升序进行排序,默认的排序方式
#DESC 按照降序进行排序
select store_name,sales,date from store order by sales asc;
select store_name,sales,date from store order by date asc;
select store_name,sales,date from store order by date desc;

9、| | 连接符
- 如果sql_mode开启开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
select store_name || ' ' || sales from store where store_name='Los Angeles';
select store_name || ' ' || date from store where store_name='Los Angeles';

10、GROUP BY
- BY后面的栏位的查询结果进行汇总分组,通常是结合聚合函数一起使用的
- GROUP BY 有一个原则,就是 SELECT 后面的所有列中,没有使用聚合函数的列,必须出现在GROUP BY后面。
- 语法:SELECT 字段1,SUM(字段2) FROM 表名 GROUP BY 字段1;
select site,sum(money) from FARE group by site;
select site,sum(money),date from FARE group by site order by money desc;
select site,count(money),sum(money),date from FARE group by site order by money desc;
11、HAVING
- 用来过滤由GROUP BY语句返回的记录集,通常与GROUP BY语句联合使用。
- HAVING语句的存在弥补了WHERE关键字不能与聚合函数联合使用的不足。如果被SELECT的
- 只有函数栏,那就不需要GROUP BY子句。
- 语法:SELECT 字段1,SUM(字段2) FROM 表名 GROUP BY 字段1 HAVING(函数条件);
select site,count(money),sum(money),date from FARE group by site having sum(money) >=700;
二、函数
1、数学函数
| abs(x) | 返回 x 的绝对值 |
|---|---|
| rand() | 返回 0 到 1 的随机数 |
| mod(x,y) | 返回 x 除以 y 以后的余数 |
| power(x,y) | 返回 x 的 y 次方 |
| round(x) | 返回离 x 最近的整数 |
| round(x,y) | 保留 x 的 y 位小数四舍五入后的值 |
| sqrt(x) | 返回 x 的平方根 |
| truncate(x,y) | 返回数字 x 截断为 y 位小数的值 |
| ceil(x) | 返回大于或等于 x 的最小整数 |
| floor(x) | 返回小于或等于 x 的最大整数 |
| greatest(x1,x2…) | 返回集合中最大的值 |
| least(x1,x2…) | 返回集合中最小的值 |
select abs(-1),rand(),mod(5,3),power(2,3),round (1.579),round(1.734,2);
select sqrt(9),truncate(1.234,2),ceil(1.2),floor(1.9),greatest(1,2,3,4),least(1,2,3,4);

2、聚合函数
| avg() | 返回指定列的平均值 |
|---|---|
| count() | 返回指定列中非 NULL 值的个数 |
| min() | 返回指定列的最小值 |
| max() | 返回指定列的最大值 |
| sum(x) | 返回指定列的所有值之和 |
select avg(money) from FARE;
select count(money) from FARE;
select min(money) from FARE;
select max(money) from FARE;
select sum(money) from FARE;
- #count(*)包括所有列的行数,在统计结果时,不好忽略值为null
- #count(字段)只包括那一行的列数,在统计结果的时候,会忽略列值为null的值
3、字符串函数
| trim() | 返回去除指定格式的值 |
|---|---|
| concat(x,y) | 将提供的参数 x 和 y 拼接成一个字符串 |
| substr(x,y) | 获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同 |
| substr(x,y,z) | 获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串 |
| length(x) | 返回字符串 x 的长度 |
| replace(x,y,z) | 将字符串 z 替代字符串 x 中的字符串 y |
| upper(x) | 将字符串 x 的所有字母变成大写字母 |
| lower(x) | 将字符串 x 的所有字母变成小写字母 |
| left(x,y) | 返回字符串 x 的前 y 个字符 |
| right(x,y) | 返回字符串 x 的后 y 个字符 |
| repeat(x,y) | 将字符串 x 重复 y 次 |
| space(x) | 返回 x 个空格 |
| strcmp(x,y) | 比较 x 和 y,返回的值可以为-1,0,1 |
| reverse(x) | 将字符串 x 反转 |
SELECT TRIM ([ [位置] [要移除的字符串] FROM ] 字符串);
- #[位置]:的值可以为 LEADING (起头), TRAILING (结尾), BOTH (起头及结尾)。
- #[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。
select trim(leading 'na' from 'nanchang');
select trim(trailing '--' from 'nanchang--');
select trim(both '--' from '--nanchang--');
select concat(region,site) from REGION where region = 'south';
select concat(region,' ',site) from REGION where region = 'south';
select substr(money,1,2) from FARE;
select length(site) from FARE;
select replace(site,'ji','--') from FARE;
select upper(site) from FARE;
select lower('HAHAHA');
select left(site,2) from FARE;
select right(site,3) from FARE;
select repeat(site,2) from FARE;
select space(2);
select strcmp(100,200);
select reverse(site) from FARE;
4、日期时间函数
| 日期时间函数 | 描述 |
|---|---|
| curdate() | 返回当前时间的年月日 |
| curtime() | 返回当前市价你的时分秒 |
| now() | 返回当前时间的日期和时间 |
| month(x) | 返回日期x中的月份值 |
| week(x) | 返回日期x是年度的第几个周 |
| hour(x) | 返回x中的小时值 |
| minute(x) | 返回日期x中的分钟值 |
| second(x) | 返回日期x中的秒数值 |
| dayotweek(x) | 返回x是星期几,1为星期日,2为星期一 |
| replace(x,y,z) | 将字符z替代字符串x中的字符串y |
| dayotmonth(x) | 计算日期x是本月的第几天 |
| dayotyear(x) | 计算日期x是本年的第几天 |
select dayofweek(curtime());
select dayofmonth(curtime());
select dayofyear(curtime());
select curdate();
select curtime();
select now();



















