实验准备:
第一张表:
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
---- SELECT ----显示表格中一个或数个字段的所有数据记录
语法:SELECT "字段" FROM "表名";
eg:
在store_info表中查找store_name的记录
select Store_Name from store_info;
+-------------+
| Store_Name |
+-------------+
| Los Angeles |
| Houston |
| Los Angeles |
| Boston |
+-------------+
在store_info表中查找store_name,sales,date的记录(字段顺序无所谓)
select store_name,sales,date from store_info;
+-------------+-------+------------+
| store_name | sales | date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Houston | 250 | 2020-12-07 |
| Los Angeles | 300 | 2020-12-08 |
| Boston | 700 | 2020-12-08 |
+-------------+-------+------------+
DISTINCT
---- DISTINCT ----不显示重复的数据记录
语法:SELECT DISTINCT "字段" FROM "表名";
eg:
select distinct store_name from store_info;
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Boston |
+-------------+
select distinct store_name,date from store_info;
+-------------+------------+
| store_name | date |
+-------------+------------+
| Los Angeles | 2020-12-05 |
| Houston | 2020-12-07 |
| Los Angeles | 2020-12-08 |
| Boston | 2020-12-08 |
---- WHERE ----有条件查询
语法:SELECT "字段" FROM "表名" WHERE "条件";
eg:
查找sales大于1000的store_name
select store_name from store_info where sales>1000;
+-------------+
| store_name |
+-------------+
| Los Angeles |
+-------------+
查找store_name为“Houston”的记录
select store_name,date,sales from store_info where store_name='Houston';
+------------+------------+-------+
| store_name | date | sales |
+------------+------------+-------+
| Houston | 2020-12-07 | 250 |
---- AND OR ----且 或
语法:SELECT "字段" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"}+ ;
eg:
查找sales大于500且sales小于1000的store_name,sales,date
select store_name,sales,date from store_info where sales >500 and sales<1000;
+------------+-------+------------+
| store_name | sales | date |
+------------+-------+------------+
| Boston | 700 | 2020-12-08 |
+------------+-------+------------+
查找sales大于500且sales小于1000,或者sales大于1000的store_name,sales,date(注意把括号里的当成一个整体,并且优先执行)
select store_name,sales,date from store_info where (sales >500 and sales<1000) or sales > 1000;
+-------------+-------+------------+
| store_name | sales | date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Boston | 700 | 2020-12-08 |
+-------------+-------+------------+
---- IN ----显示已知的值的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" IN ('值1', '值2', ...);
eg:
在store_info表中查找store_name为Los Angeles或者Houston的字段
select * from store_info where store_name in ('Los Angeles','Houston');
+-------------+-------+------------+
| Store_Name | Sales | Date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Houston | 250 | 2020-12-07 |
| Los Angeles | 300 | 2020-12-08 |
+-------------+-------+------------+
在store_info表中查找store_name不为Los Angeles或者Houston的字段
select * from store_info where store_name not in ('Los Angeles','Houston');
+------------+-------+------------+
| Store_Name | Sales | Date |
+------------+-------+------------+
| Boston | 700 | 2020-12-08 |
---- BETWEEN ----显示两个值范围内的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" BETWEEN '值1' AND '值2';
eg:
在store_info表中查找时间在2020-12-06到2020-12-11的数据
select * from store_info where date between '2020-12-06' and '2020-12-11';
---- 通配符 ----通常通配符都是跟 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' 则不符合这个模式。
---- LIKE ----匹配一个模式来找出我们要的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" LIKE {模式};
eg:
在store_info表中查找所有包含‘on’的数据
select * from store_info where store_name like '%on%'