2021-07-22 SQL Day6

一.数据过滤

image.png

1.通配符 - 下划线( _ )

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '奶_';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '薯_';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '方_面';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '方__';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '奶_糖';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '__糖';
image.png

image.png

image.png

image.png

image.png

image.png

2.通配符 - 百分号( % )

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '%糖';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '方%面';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '方便%面';

SELECT * FROM milk_tea AS m WHERE m.prod_name LIKE '奶%';
image.png

image.png

image.png

image.png

image.png

3.“且”操作 - AND

SELECT * FROM milk_tea AS m WHERE m.sale_price >= 5;
SELECT * FROM milk_tea AS m WHERE m.sale_price <= 15;
image.png
SELECT * FROM milk_tea AS m WHERE m.sale_price >= 5 AND m.sale_price <= 15;
image.png
SELECT * FROM milk_tea AS m WHERE m.sale_price >= 5 AND m.sale_price <= 15 AND m.sale_price IS NOT NULL;
image.png
SELECT * FROM milk_tea AS m WHERE m.sale_price >= 5 AND m.sale_price <= 15 AND m.prod_name LIKE '薯_';
image.png

4.“或”操作 - OR

SELECT * FROM milk_tea AS m WHERE m.sale_price >= 5 OR m.sale_price <= 15;
image.png
SELECT * FROM milk_tea AS m WHERE m.sale_price >= 5 AND m.sale_price <= 15 OR m.sale_price BETWEEN 1 AND 5;
image.png
SELECT * FROM milk_tea AS m WHERE m.sale_price >= 5 AND m.sale_price <= 15 OR m.sale_price IS NULL;
image.png
SELECT * FROM milk_tea AS m WHERE m.prod_name = '奶茶' OR m.prod_name = '薯片' OR m.prod_name = '棒棒糖';
image.png

image.png

5.取值限制 - IN

SELECT * FROM milk_tea AS m WHERE m.prod_name IN ('奶茶','薯片','棒棒糖');

SELECT * FROM milk_tea AS m WHERE NOT m.prod_name IN ('奶茶','薯片','棒棒糖');

SELECT * FROM milk_tea AS m WHERE m.in_price IN (10.8, 2.1, 9.3);
image.png

image.png

6.否定条件 - NOT

SELECT * FROM milk_tea AS m WHERE NOT m.prod_name IN ('奶茶','薯片','棒棒糖');
image.png
SELECT * FROM milk_tea AS m WHERE NOT m.prod_name = '奶茶' AND NOT m.prod_name = '薯片';
image.png

二.课后作业

Q1:写出 SQL 语句中的通配符,及其用法。
答:1.下划线_:代表任意字符,且能代表的字符数仅为1。必须知道要代表的字符具体的位置。
2.百分号%:代表任意字符,且能代表的字符数为任意多个,包括0。

Q2:写出模糊查询的具体结构。
答:关键字(WHERE)+ 查询的列(字段名)+ 关键字(LIKE)+ ‘匹配文本’(含通配符)

Q3:写出以下语句。
1、从 milk_tea 表中找到产品名是‘薯’开头或者‘糖’结尾的。
2、从 milk_tea 表中找到产品名是‘薯’开头或者‘糖’结尾的,且产品名称只有 2 个字。
3、从 milk_tea 表中找到产品名是‘奶’开头并且‘茶’结尾的。
4、从 milk_tea 表中找到销售价格不大于 10 的产品。(2 种写法)

SELECT * FROM milk_tea;

SELECT m.prod_name FROM milk_tea AS m WHERE m.prod_name LIKE '薯%' OR m.prod_name LIKE '%糖';

SELECT m.prod_name FROM milk_tea AS m WHERE m.prod_name LIKE '薯_' OR m.prod_name LIKE '_糖';


SELECT m.prod_name FROM milk_tea AS m WHERE m.prod_name LIKE '奶%茶';
SELECT m.prod_name FROM milk_tea AS m WHERE m.prod_name LIKE '奶%' AND m.prod_name LIKE '%茶';

SELECT m.* FROM milk_tea AS m WHERE m.sale_price <= 10;
SELECT m.* FROM milk_tea AS m WHERE m.sale_price BETWEEN 0 AND 10;

你可能感兴趣的:(2021-07-22 SQL Day6)