学习前首先创建两张表,用于语句实验
mysql -uroot -p
show databases;
create database zhuzhu;
use train_ticket;
create table REGION(region varchar(10),site varchar(20));
create table FARE(site varchar(20),money int(10),date varchar(15));
desc REGION;
desc FARE;
insert into REGION values ('south','changsha'); ## REGION(地区)
insert into REGION values ('south','nanchang');
insert into REGION values ('north','beijing');
insert into REGION values ('north','tianjin');
insert into FARE values ('changsha',1000,'2021-01-30'); ## FARE(票价)
insert into FARE values ('nanchang',700,'2021-01-30');
insert into FARE values ('beijing',1500,'2021-01-30');
insert into FARE values ('tianjin',1200,'2021-01-30');
insert into FARE values ('beijing',2200,'2021-02-05');
select * from REGION;
select * from FARE;
语法:SELECT "栏位" FROM "表名";
SELECT region from REGION
语法: SELECT DISTINCT "栏位" FROM "表名";
SELECT DISTINCT "region"FROM "REGION";
语法:SELECT 字段 FROM 表名 WHERE 条件
select site from FARE where money>1000;
select site,money from FARE where money>1000;
SELECT "栏位" FROM "表名" WHERE "条件1" {
[AND|OR]"条件2"}+;
select site,money from FARE where money > 1000 and money < 2200;
select site,money from FARE where money > 1200 or money < 1000;
select site,money,date from FARE where money >= 500 and (date < '2021-02-05' and money < 1000);
语法:SELECT "栏位" FROM "表名" WHERE "栏位" IN ('值1','值2',...);
select * from FARE where site in ('tianjin','beijing');
语法: SELECT "栏位" FROM "表名" WHERE "栏位" BETWEEN '值1' AND '值2';
select site,money from FARE where money between '900' and '2100';
%:百分号表示零个、一个或多个字符
_:下划线表示单个字符
LIKE:用于匹配模式来查找资料
语法:SELECT 字段 FROM 表名 WHERE 字段 LIKE ‘模式’;
select * from FARE where site like 'be%';
select site,money from FARE where site like '%jin_';
按关键字排序
语法:SELECT 字段 FROM 表名 [WHERE 条件] ORDER BY 字段 [ASC,DESC];
#ASC:按照升序进行排序,默认的排序方式
#DESC:按照降序进行排序
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.89);
SELECT round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1);
select abs(-1),rand(),mod(5,3),power(2,3),round (1.579),round(1.734,2);
avg() 返回指定列的平均值
count() 返回指定列中非 NULL 值的个数
min() 返回指定列的最小值
max() 返回指定列的最大值
sum(x) 返回指定列的所有值之和
#count(*)包括了所有的列的行数,在统计结果的时候,不会忽略值为NULL
#count(列名)只包括列名那一列的行数,在统计结果的时候,会忽略列值为NULL的行
例:
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的值
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 (起头及结尾)。
#[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。
⚪:补充:如sql_mode开启开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
select region || ' ' || site from REGION where region = 'north';
select site || ' ' || money || ' ' || date from FARE;
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;
语法: 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;
语法: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;
语法:SELECT "表格別名"."栏位1" [AS] "栏位別名" FROM "表格名" [AS] "表格別名";
例: ##AS可以加也可以不加
select RE.region AS reg, count(site) from REGION AS RE group by reg;
select FA.site AS si,sum(money),count(money),date AS da from FARE AS FA group by si;
在WHERE 子句或HAVING 子句中插入另一个SQL 语句
语法:
#外查询
SELECT "栏位1" FROM "表格1" WHERE "栏位2" [比较运算符]
#内查询
(SELECT "栏位1" FROM "表格2" WHERE "条件");
#可以是符号的运算符,例如:=、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
例如:
select A.site,region from REGION AS A where A.site in(select B.site from FARE AS B where money<2000);
select B.site,money,date from FARE AS B where site in(select A.site from REGION AS A where region = 'north');
语法:SELECT "栏位1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
例:
select region from REGION where exists (select * from FARE where money = '700');
下一篇继续讲解mysql高级语句