准备环境
mysql -uroot -p
create database kgc;
use kgc;
create table location(region varchar(10),store_name char(20)); # 表一结构
create table store_info(storm_name char(20),sales int(10),date char(15)); #表二结构
# 表一数据内容
insert into location values ('East','Bostion');
insert into location values ('East','New York');
insert into location values ('West','Los Angeles');
insert into location values ('West','Houstion');
# 表二数据内容
insert into store_info values('Los Angeles',1500,'2023-01-30');
insert into store_info values('Los Angeles',300,'2023-01-30');
insert into store_info values('Houstion',200,'2023-01-30');
insert into store_info values('Bostion',700,'2023-01-30');
select * from region;
select * from fare;
显示表格中一个或数个字段的所有资料
语法:select 字段 from 表名
select region from store_info;
不显示重复的数据记录
语法:select distinct ‘字段’ from ‘表名’;
select distinct storm_name from store_info ;
有条件查询
语法:select “字段” from"表名" where ‘条件’;
select storm_name from store_info where sales > 1000;
==**且 ,或
语法:select “字段” from “表名” where “条件1” {[and|or] “条件2”}+ ;
select sales from store_info where sales > 500 and sales < 1000;
select sales from store_info where sales > 500 or sales < 1000;
显示已知的值的数据记录
语法:select “字段” from “表名” where “字段” in (‘值1’, ‘值2’, …);
select * from store_info where storm_name in (‘Los Angeles’,‘Houston’);
显示两个值范围内的数据记录
语法:select “字段” from “表名” where “字段” between ‘值1’ and ‘值2’;
select * from store_info where date between ‘2020-12-05’ and ‘2020-12-10’;
通常通配符都是跟 LIKE 一起使用的
like:用于匹配模式来查找资料
语法:select “字段” from “表名” where “字段” like{模式};
select * from store_info where storm_name like ‘Los%’;
select storm_name from store_info where storm_name like ‘Los%’;
按关键字排序
语法:select “字段” from “表名” [where “条件”] order by “字段”[ASC, DESC];
select sales,date,storm_name from store_info order by sales desc;
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 字段1,sum(字段2) from表名 group by 字段1;
select store_name,sum(sales) from store_info select 字段1,wum(字段2) from 表名 group by 字段1;
11、having
语法:select 字段1,sum(字段2) from 表名 group by 字段1 having(函数条件);
select site,count(money),sum(money),date from from group by site having sum(money) >=700;
字段别名、表格别名
select location.store_name from location,store_info where location.store_name = store_info.store_name;
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL 语句
SELECT 字段1 FROM 表格1 WHERE 字段2 [比较运算符] #外查询
(SELECT 字段1 FROM 表格2 WHERE 条件) #内查询
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');
环境准备
use kgc;
create table location (Region char(20),Store_Name char(20));
insert into location values('East','Boston');
insert into location values('East','New York');
insert into location values('West','Los Angeles');
insert into location values('West','Houston');
create table store_info (Store_Name char(20),Sales int(10),Date char(10));
insert into store_info values('Los Angeles','1500','2020-12-05');
insert into store_info values('Houston','250','2020-12-07');
insert into store_info values('Los Angeles','300','2020-12-08');
insert into store_info values('Boston','700','2020-12-08');
select * from location;
select * from store_Info
内连接1 inner join
语法:select 字段 from 表名1 表别名1 inner join 表名2 表别名2 on 表别名1.联结字段 = 表别名2.联结字段;
例:
select * from location A inner join store_info B on A.store_name = B.store_name ;
== 内连接2 inner join(using)函数
语法:select 字段 from 表名1 inner join 表名2 using(联结字段);
例:
select * from location inner join store_info using(store_name}; #必须保证要查询的交集字符字段名一样才可以
左连接 left join
语法:select 字段 from 表1 表别名1 left join 表2 表别名2 on 表别名1.联结字段1 = B.联结字段2 ;
例:
select * from location A left join store_info B on A.store_name = B.store_name ;
右连接 right join
语法:select 字段 from 表1 表别名1 right join 表2 表别名2 on 表别名1.联结字段1 = 表别名2.联结字段2;
例:
select * from location A right join store_info B on A.store_name = B.store_name ;
多表查询
语法:
select 字段 from 表1,表2 where 表1.联结字段 = 表2.联结字段;
视图,可以被当作是虚拟表或存储查询
语法:create view "视图表名" AS "select 语句";
例子
create view v_aa AS select A.region region sum (B.sales) sales from location A inner join store_info B on A.store_name = B.store_name group by region;
将两个SQL语句的结果合并起来,两个SQL语句产生的字段需要是同样的资料种类
语法:
select 语句1 union select 语句2;
语法:select 语句1 union all select 语句2;
取两个SQL语句结果的交集
显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,还不能重复
是 SQL 用来做为 if-then-else之类逻辑的关键字
语法:
select case("字段名")
--> when "条件1" then "结果1"
--> when "条件2" then "结果2"
...
[lese "结果N"]
END
FROM "表名";
# "条件" 可以是一个数值或是公式。 ELSE 子句则并不是必须的。
例子:
select store_name, case store_name
when 'Los Angeles' then sales * 2
when 'Boston' then 2000
else sales end "New sales",date from store_Info;
#"New sales" 是用于 case那个字段的字段名。
(1)使用length函数查看字符的长度
select length(null), length(‘1234’), length(‘’);
(2)查看表里面字符为null的行和不为null的行
语法:
select 字段 from 表名 where 字段 is null;
select 字段 from 表名 where 字段 is not null;
例:
select * from cs where name is null;
select * from cs where name is not null;
(3)查看空值的行和不为空值的行
语法:
select 字段 from 表名 where 字段 = '';
select 字段 from 表名 where 字段 != '';
select 字段 from 表名 where 字段 <> '';
例:
select * from cs where name = '';
select * from cs where name <> '';
select * from cs where name != '';
(4)统计所有的行数和忽略为null值的行
语法:
select count(*) from 表名;
select count(name) from 表名;
例子
select count(*) from cs;
select count(name) from cs;
语法:
select "字段" from "表名" where "字段" regexp {模式};
(1)匹配以es结尾的字符串的字段
select * from store_info where store_name regexp ‘es$’;
(2)匹配字符串里包含to的字段
select * from store_info where store_name regexp ‘to’;
(3)匹配字符串以A-G开头的字段
select * from store_info where store_name regexp ’ ^ [A-G] ’ ;
(4)匹配字符串以 HO | Bo 开头的字段
select * from store_info where store_name regexp ‘Ho|Bo’;
语法:
delimiter $$ #将语句的结束符号从分号;临时改为两个$$(可以是自定义)
create procedure cs() #创建存储过程,过程名为Proc,不带参数
-> begin #过程体以关键字 begin开始
-> select * from store_info; #过程体语句
-> end $$ #过程体以关键字 end 结束
delimiter ; #将语句的结束符号恢复为分号
##调用存储过程##
call cs;
语法:
DROP PROCEDURE IF EXISTS Proc; #仅当存在时删除,不添加 IF EXISTS 时,如果指定的过程不存在,则产生一个错误
(2)循环语句while ···· end while
DELIMITER $$
CREATE PROCEDURE proc3()
-> begin
-> declare var int(10);
-> set var=0;
-> while var<6 do
-> insert into t values(var);
-> set var=var+1;
-> end while;
-> end $$
DELIMITER ;
CALL Proc3;