数据库实例准备:
create database kgc;
use kgc;
###在数据库kgc中创建数据表location,并添加数据记录
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');
###在数据库kgc中创建数据表store_info,并添加数据记录
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:显示表格中一个或数个字段的所有数据记录
语法:SELECT "字段" FROM "表名";
select store_name from store_info;
distinct:不显示重复的数据记录
语法:SELECT DISTINCT "字段" FROM "表名";
select distinct store_name from store_info;
where:有条件查询
语法:SELECT "字段" FROM "表名" WHERE "条件";
select * from store_info where sales > 1000;
and or:且 或
语法:SELECT "字段" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"}+ ;
select * from store_info where sales > 1000 or (sales >=200 and sales <=500);
in:显示已知的值的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" IN ('值1', '值2', ...);
select * from store_info where store_name in ('Houston','Los Angeles');
between:显示两个值范围内的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" BETWEEN '值1' AND '值2';
SELECT * FROM Store_Info WHERE Date BETWEEN '2020-12-06' AND '2020-12-10';
通配符:通常通配符都是跟like一起使用的
% :百分号表示零个、一个或多个字符
_ :下划线表示单个字符
'A_Z':所有以 'A' 起头,另一个任何值的字符,且以'Z'为结尾的字符串。例如,'ABZ' 和 'A2Z' 都符合这一个模式,而 'AKKZ' 并不符合 (因为在A和Z之间有两个字符,而不是一个字符)。
'ABC%': 所有以 'ABC' 起头的字符串。例如,'ABCD' 和 'ABCABC' 都符合这个模式。
'%XYZ': 所有以 'XYZ' 结尾的字符串。例如,'WXYZ' 和 'ZZXYZ' 都符合这个模式。
'%AN%': 所有含有 'AN'这个模式的字符串。例如,'LOS ANGELES' 和 'SAN FRANCISCO' 都符合这个模式。
'_AN%':所有第二个字母为 'A' 和第三个字母为 'N' 的字符串。例如,'SAN FRANCISCO' 符合这个模式,而 'LOS ANGELES' 则不符合这个模式。
如: select * from store_info where store_name like '__us%';
select * from store_info where store_name like '_os%';
like:匹配一个模式来找出我们要的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" LIKE {模式};
select * from store_info where store_name like '%on%';
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 rand() * 1000;
###以下获取1000以内随机整数的两种方法
select truncate(rand() * 1000,0);
select round(rand() * 1000);
###返回指定列sales的平均值
select avg(sales) from store_info;
###返回指定列store_name列中非NULL值的个数
select count(store_name) from store_info;
select count(distinct store_name) from store_info;
select concat(region,store_name) from location where store_name='Los Angeles';
select concat(region,' ',store_name) from location where store_name='Los Angeles';
#如sql_mode开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
select region || ' ' || store_name from location where store_name='Los Angele
select substr(store_name,3) from location where store_name='Los Angeles';
select * from location;
select substr(store_name,3,4) from location where store_name='Los Angeles';
select trim([ [位置] [要移除的字符串] from ] 字符串);
#[位置]:的值可以为leading(起头),trailing(结尾),both(起头及结尾)。
#[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。
select trim(leading 'Ne' from 'New York');
select trim(trailing 'aw' from 'Newaw Yorkaw');
select trim(both 'aw' from 'awNewaw Yorkaw');
select region,length(store_name) from location;
select replace(region,'ast','astern') from location;
order by:按关键字排序
语法:SELECT "字段" FROM "表名" [WHERE "条件"] ORDER BY "字段" [ASC, DESC];
#ASC是按照升序进行排序的,是默认的排序方式。
#DESC是按降序方式进行排序。
select * from store_info order by sales desc;
group by:对group by后面字段的查询结果,进行汇总分组,通常是结合聚合函数一起使用的
语法:SELECT "字段1", SUM("字段2") FROM "表名" GROUP BY "字段1";
select store_name,sum(sales) from store_info group by store_name order by sales desc;
**注意:** **group by有一个原则,凡是在group by后面出现的字段,必须在select后面出现;**
凡是在select后面出现的、且未在聚合函数中出现的字段,必须出现在group by后面出现。
sql语句执行顺序from >> on >> join >> where >> group by >> having >> distinct >> order by >> limit.
**having:用来过滤由group by语句返回的记录集,通常与group by语句联合使用
**
语法:SELECT "字段1", SUM("字段2") FROM "表格名" GROUP BY "字段1" HAVING (函数条件);
select store_name,sum(sales) from store_info group by store_name having sum(sales) > 1000;
注意:
having语句的存在弥补了where关键字不能与聚合函数联合使用的不足。
别名:字段別名 表格別名
语法:SELECT "表格別名"."字段1" [AS] "字段別名" FROM "表格名" [AS] "表格別名";
select A.store_name store,sum(A.sales) "total sales" from store_info A group p by A.store_name;
子查询:连接表格,在WHERE 子句或 HAVING 子句中插入另一个SQL语句
语法:SELECT "字段1" FROM "表格1" WHERE "字段2" [比较运算符] #外查询
(SELECT "字段1" FROM "表格2" WHERE "条件"); #内查询
#可以是符号的运算符,例如 =、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
select sum(sales) from store_info where store_name in (select store_name from location where region='West');
select sum(A.sales) from store_info A where A.store_name in (select store_name from location B where B.store_Name = A.store_name);
exists:用来测试内查询有没有产生任何结果,类似布尔值是否为真
#如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。
语法:SELECT "字段1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
select sum(sales) from store_info where exists (select * from location where region = 'West');
###图示二sql语句内连接表示
select * from location A inner join store_info B on A.store_name = B.store_name; #解法一
select * from location A inner join store_info B using(store_name); #解法二
select * from location A,store_info B where A.store_name=B.store_name; #解法三多表连接,没有以上两种解法效率高
select region,sum(B.sales) from location A,store_info B where A.store_name=B.store_name group by(region) order by sum(B.sales);
select store_name from location where store_name in (select store_name from store_info); #解法四子查询
###图示二sql语句左连接表示
select * from location A left join store_info B on A.store_name = B.store_name;
###图示二sql语句右连接表示
select * from location A right join store_info B on A.store_name = B.store_name;
view:视图,可以被当作是虚拟表或存储查询
视图跟表格的不同是,表格中有实际储存数据记录,而视图是建立在表格之上的一个架构,它本身并不实际储存数据记录。
临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。
CREATE VIEW "视图表名" AS "SELECT 语句";
例如
create view V_AB as select region,sum(B.sales) total_sales from location A,store_info B where A.store_name=B.store_name group by(region) order by(total_sales) desc;
show tables;
desc V_AB;
select * from V_AB;
select * from V_AB order by(total_sales) asc; #直接对视图进行升序操作
注意:
如果视图中保存的是原表字段,就可以对视图数据进行更改,也就是更改原数据表中的字段属性值;否则,视图中保存的是做过处理后的数据表记录,即经过group by或sum等函数处理后的字段,那么则不可以对视图进行更改。
联集:将两个SQL语句的结果合并起来,两个SQL语句所产生的字段需要是同样的数据记录种类
###上图联集的SQL语句表示
UNION :生成结果的数据记录值将没有重复,且按照字段的顺序进行排序
语法:[SELECT 语句 1] UNION [SELECT 语句 2];
UNION ALL :将生成结果的数据记录值都列出来,无论有无重复
语法:[SELECT 语句 1] UNION ALL [SELECT 语句 2];
select store_name from location union select store_name from store_info;
select store_name from location union all select store_name from store_info;