MySQL高级语句(一)

2021/9/5 19:40:03

本文主要是介绍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、字符串函数

 

准备工作

1、安装MySQL数据库

2、实验准备,数据表配置

 1 mysql -uroot -p
 2 show databases;
 3 
 4 create database train_ticket;
 5 use train_ticket;
 6 create table REGION(region varchar(10),site varchar(20));
 7 create table FARE(site varchar(20),money int(10),date varchar(15));
 8 
 9 desc REGION;
10 desc FARE;
11 
12 insert into REGION values ('south','changsha');
13 insert into REGION values ('south','nanchang');
14 insert into REGION values ('north','beijing');
15 insert into REGION values ('north','tianjin');
16 
17 insert into FARE values ('changsha',1000,'2021-01-30');
18 insert into FARE values ('nanchang',700,'2021-01-30');
19 insert into FARE values ('beijing',1500,'2021-01-30');
20 insert into FARE values ('tianjin',1200,'2021-01-30');
21 insert into FARE values ('beijing',2200,'2021-02-05');
22 
23 select * from REGION;
24 select * from FARE;

一、MySQL高级进阶SQL 语句

1、SELECT

显示表格中一个或数个字段的所有资料
语法:SELECT 字段 FROM 表名

select distinct region from REGION;

2、DISTINCT

不显示重复的资料(去重)
语法:SELECT DISTINCT 字段 FROM 表名

select distinct region from REGION;

3、WHERE

有条件查询
语法:SELECT 字段 FROM 表名 WHERE 条件

select site from FARE where money > 1000;
select site from FARE where money < 1000;
select site from FARE where money = 1000;

4、AND、OR
and(并且)、or(或者)
语法:SELECT 字段 FROM 表名 WHERE 条件1 ([AND|OR] 条件2)+;

select site from FARE where money > 1000 and (money < 1500);

select site,money from FARE where money < 500 or (money < 1500 and money >= 700);

select site,money,date from FARE where money >= 500 and (date < '2021-02-05' and money < 1000);

5、IN

显示已知的值的资料
语法:SELECT 字段 FROM 表名 WHERE 字段 IN (‘值1’,‘值2’,……);

select site,money from FARE where money in (700,1000);

6、BETWEEN

显示两个值范围内的资料
语法:SELECT 字段 FROM 表名 WHERE 字段 BETWEEN ‘值一’ and ‘值二’;

select * from FARE where money between 500 and 1000;

7、通配符、LIKE

通常通配符都是跟LIKE一起使用

 

 LIKE:用于匹配模式来查找资料
语法:SELECT 字段 FROM 表名 WHERE 字段 LIKE ‘模式’;

select * from FARE where site like 'be%';
select site,money from FARE where site like '%jin_';

8、ORDER BY

按关键字排序
语法:SELECT 字段 FROM 表名 [WHERE 条件] ORDER BY 字段 [ASC,DESC];

select * from FARE order by money desc;
select date,money from FARE order by money desc;

9、| | 连接符

  • 如果sql_mode开启开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
mysql -uroot -p
use train_ticket;

select region || ' ' || site from REGION where region = 'north';
select site || ' ' || money || ' ' || date from FARE;

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、数学函数

 

 

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、聚合函数

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、字符串函数

SELECT TRIM ([ [位置] [要移除的字符串] FROM ] 字符串);
  • #[位置]:的值可以为 LEADING (起头), TRAILING (结尾), BOTH (起头及结尾)。
  • #[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。
 1 select trim(leading 'na' from 'nanchang');
 2 select trim(trailing '--' from 'nanchang--');
 3 select trim(both '--' from '--nanchang--');
 4 
 5 select concat(region,site) from REGION where region = 'south';
 6 select concat(region,' ',site) from REGION where region = 'south';
 7 
 8 select substr(money,1,2) from FARE;
 9 
10 select length(site) from FARE;
11 
12 select replace(site,'ji','--') from FARE;
13 
14 select upper(site) from FARE;
15 
16 select lower('HAHAHA');
17 
18 select left(site,2) from FARE;
19 
20 select right(site,3) from FARE;
21 
22 select repeat(site,2) from FARE;
23 
24 select space(2); 
25 
26 select strcmp(100,200);
27 
28 select reverse(site) from FARE;

 

  

  

  

  

  

  

  

  

  

  

  

  



这篇关于MySQL高级语句(一)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程