SQL statement (DQL)

DQL preparation and grammar

Ready to work

#创建商品表:
create table product(
pid int primary key,
pname varchar(20),
price double,
category_id varchar(32)
);
INSERT INTO product(pid,pname,price,category_id) VALUES(1,'联
想',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(2,'海
尔',3000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(3,'雷
神',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(4,'JACK
JONES',800,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(5,'真维
斯',200,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(6,'花花公
⼦',440,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(7,'劲
霸',2000,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(8,'⾹奈
⼉',800,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(9,'相宜本
草',200,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(10,'⾯
霸',5,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(11,'好想你
枣',56,'c004');
INSERT INTO product(pid,pname,price,category_id) VALUES(12,'⾹飘飘奶
茶',1,'c005');
INSERT INTO product(pid,pname,price,category_id) VALUES(13,'果9',1,NULL);

grammar

select [distinct]
* | 列名,列名
fromwhere 条件

Simple query

-- 查询所有的商品.
SELECT * from product;
-- 查询商品名和商品价格.
select pname,price from product;
-- 查询价格,去掉重复值.
select DISTINCT price from product;
-- 查询结果是表达式(运算查询):将所有商品的价格+10元进⾏显示.
select pname,price+10 from product;
-- 别名查询.使⽤的关键字是as(as可以省略的).列别名
select pname,price+10 as '价格' from product;
select pname,price+10 '价格' from product;
-- 别名查询.使⽤的关键字是as(as可以省略的).表别名
select * from product as p;
select * from product p;

Condition query

Comparison operator < <= > >= =<> Greater than, less than, greater than (less than) equal to, not equal to
BETWEEN…AND… The value displayed in the in list, for example: in(100,200)
LIKE'character' Fuzzy query, in the like statement,% represents zero or more arbitrary characters, and _ represents one character, for example: first_name like'_a%';
IS NULL Determine whether it is empty
Logical operator and Multiple conditions are met at the same time
or Multiple conditions can be fulfilled
not Not successful, for example: where not(salary>100);
#查询商品名称为“花花公⼦”的商品所有信息:
select * from product where pname='花花公⼦';
#查询价格为800商品
select * from product where price=800;
#查询价格不是800的所有商品
select * from product where price<>800;
select * from product where price!=800; -- mysql特有的符号
#查询商品价格⼤于60元的所有商品信息
select * from product where price>60;
#查询商品价格在200到1000之间所有商品
select * from product where price>=200 and price<=1000;
select * from product where price between 200 and 1000;
#查询商品价格是200或800的所有商品
select * from product where price=200 or price=800;
select * from product where price in(200,800);
# LIKe 中的 %代表匹配任意⻓度的任意字符; _代表匹配⼀个任意字符
#查询商品名称含有'霸'字的所有商品
select * from product where pname like '%霸%';
#查询商品名称以'⾹'开头的所有商品
select * from product where pname like '⾹%';
#查询商品名称第⼆个字为'想'的所有商品
select * from product where pname like '_想%';
#商品没有分类id的商品
select * from product where category_id is NULL;
#查询有分类id的商品
select * from product where category_id is NOT NULL;

Sort query

Through the order by statement, you can sort the results of the query. Temporarily placed at the end of the select statement.

  • format:
SELECT * FROM 表名 ORDER BY 排序字段 ASC|DESC;
#ASC 升序 (默认), 从⼩到⼤排序
#DESC 降序, 从⼤到⼩排序
#使⽤价格排序(降序)
select * from product order by price desc;
#在价格排序(降序)的基础上,以分类排序(降序)
select * from product order by price desc,category_id desc;
#显示商品的价格(去重复),并排序(降序)
select DISTINCT price from product order by price desc;

Aggregate query

The queries we did before were all horizontal queries. They were all judged based on the conditions. The query using the aggregate function is a vertical query. It calculates the value of a column and returns a single list. A value; in addition, the aggregate function ignores null values.
Today we learn the following five aggregate functions:

  • count: count the number of records where the specified column is not NULL;
  • sum: Calculate the numerical sum of the specified column. If the specified column type is not a numeric type, the calculation result is 0;
  • max: Calculate the maximum value of the specified column, if the specified column is a string type, then use string sorting operation;
  • min: Calculate the minimum value of the specified column, if the specified column is a string type, then use string sorting operation;
  • avg: Calculate the average value of the specified column, if the specified column type is not a numeric type, the calculation result is 0;

Exercise:

#查询商品的总条数
select count(*) from product; -- 不推荐
select count(pid) from product;
select count(category_id) from product;
#查询价格⼤于200商品的总条数
select count(*) from product where price>200;
#查询分类为'c001'的所有商品的价格总和
select sum(price) from product where category_id='c001';
#查询分类为'c002'所有商品的平均价格
select avg(price) from product where category_id='c002';
#查询商品的最⼤价格和最⼩价格
select max(price),min(price) from product;

Group query

Group query refers to the use of group by words to group query information.
Format: SELECT 字段1,字段2… FROM 表名 GROUP BY分组字段 HAVING 分组条件;
The having sub-sentence in the grouping operation is used to filter the data after grouping, and the function is similar to the where condition.

The difference between having and where:

  • Having is to filter the grouped data after the
    grouping operation is performed. Where is to filter the grouped data before the grouping operation is performed, and only the original columns of the table can be used to filter the conditions
  • You can use the aggregate function after having, and you cannot use the aggregate function after where.
  • When in a SQL statement, where there is group by \ having, execute where first, then group by, and finally having

Exercise

#统计各个分类商品的个数
select category_id,count(*) from product group by category_id;
#统计各个分类商品的个数,且只显示个数⼤于1的信息
SELECT category_id,count(*) from product GROUP BY category_id HAVING
count(*)>1;
#统计价格>200元的 各个分类商品的个数,且只显示个数⼤于1的信息
-- select * from product where price>200 group by category_id;
-- select category_id,count(*) from product where price>200 group by
category_id ;
select category_id,count(*) from product where price>200 group by
category_id HAVING count(*)>1;

Number limit query

LIMIT is a MySQL built-in function, and its function is to limit the number of query results.

格式: select * from 表名 limit m,n
其中: m是指记录开始的index,从0开始,表示第⼀条记录
n是指从第m+1条开始,取n条。
例如:
select * from tablename limit 2,4 -- 即取出第3条⾄第6条,4条记录

Application occasion: separate page

分⻚查询格式:
SELECT * FROM 表名 LIMIT startRow,pageSize;
例如:
select * from products limit 0,5; #第⼀⻚,每⻚显示5条。
select * from products limit 5,5; #第⼆⻚,每⻚显示5条。
select * from products limit 10,5; #第三⻚,每⻚显示5条。
select * from products limit startRow,5; #第curPage⻚,每⻚显示5条, startRow
的值如何计算呢?
-- 后台计算出⻚码、⻚数(⻚⼤⼩)
-- 分⻚需要的相关数据结果分析如下,
-- 注意:下⾯是伪代码不⽤于执⾏
int curPage = 2; -- 当前⻚数
int pageSize = 5; -- 每⻚显示数量
int startRow = (curPage - 1) * pageSize; -- 当前⻚, 记录开始的位置(⾏数)计算
int totalSize = select count(*) from products; -- 记录总数量
int totalPage = Math.ceil(totalSize * 1.0 / pageSize); -- 总⻚数

Guess you like

Origin blog.csdn.net/qq_43511405/article/details/108507364