Basic operation of MySQL database--DQL operation

DQL - basic query

concept

  • An important function of the database management system is data query. Data query should not simply return the data stored in the database, but also filter the data as needed and determine the format in which the data is displayed.
  • MySQL provides powerful and flexible statements to implement these operations
  • MySQL database uses select statement to implement query operation

Simplified syntax

select column name from table where condition

data preparation

create database if not exists mydb2;
use mydb2;
create table product(
pid int primary key auto_increment,
pname varchar(20) not null,
price double,
catagory_id varchar(20)
);

insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲',5000,'c001');

insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲爆休闲裤',266,'c002');
insert into product values(null,'海澜之家唯一',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');

insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');

insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,'c004');

simple query

-- --简单查询
-- --1查询所有商品

select  pid,pname ,price,catagory_id from product;
-- 或者select * from product
-- 

-- 2查询商品名和商品价格
select pname,price from product;

-- 3.1 表别名
select * from product as p;
-- 或者select * from product p;
-- select p.id,u.id from product,user;
-- 3.2 列别名
select pname as '商品名',price '商品价格' from product;
-- 4去掉重复值
select distinct price from product;
-- 或者select distinct * from product;
-- 5查询结果是表达式(运算查询):将所有商品加价十元后显示
select pname,price+10 new_price from product;

operator

  • Introduction

After the table structure of the database is determined, the meaning represented by the data in the table has been determined. By operating with MySQL operators, you can go back to data other than the table structure.

For example, there is a birth field in the student table, which indicates the student's year of birth, then subtract this field from the current time to get the actual age data of the student.

MySQL supports four operators

  • arithmetic operator
  • comparison operator
  • Logical Operators
  • bitwise operator

 arithmetic operator

+ Addition
- Subtraction
* Multiplication
/ or DIV division operation, returns the quotient
% or MOD Remainder operation, returns the remainder
arithmetic operator illustrate

comparison operator

 Logical Operators

 bitwise operator

Basic query

-- 查询商品名称为海尔洗衣机的所有商品
 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;
select * from product where not (price=800);
-- 查询商品价格大于60元的所有商品信息
select * from product where price>60;
-- 查询商品价格在200到1000之间所有商品
select * from product where price >= 200 and price <= 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);
-- 查询含有‘裤'字的所有商品
select * from product where pname like '%裤%';
-- 查询以'海'开头的所有商品
select * from product where pname like '海%';-- 百分号表示任意字符
-- 查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%';
-- 查询category_id为null的商品
select * from product where catagory_id is null;
-- 查询category_id不为null分类的商品
select * from product where catagory_id is not null;
-- 使用least求最小值
select least (10,5,20) as small_number;
select least (10,null,20) as small_number;-- 如果求最小、大值时有null,则无法比较,结果之间为null
-- 使用greatest求最大值

 select greatest (10,5,20) as max_number;

Operator Operations - Arithmetic Operators

-- 1算数运算符
select 6 + 2;
select 6 - 2;
select 6 / 2;
select 6 % 2;

-- 将所有商品加10 元
select pname,price+10 as new_price from product;
-- 将所有商品上调10%
select pname,price * 1.1 as new_price from product;

 bitwise operators (understanding)

Sort query

  • introduce

If we want to sort the read data, we can use MySQL's order by clause to set which field you want to sort by, and then return the search results.

select
字段名1,字段名2,...
from 表名
order by 字段名1 [asc|desc],字段名2[asc|desc]
  • features

1.asc means ascending order, desc means descending order , if not written, the default ascending order

2. order is used in the clause to support single field, multiple fields, expressions, functions, aliases

3. The order by clause is placed at the end of the query statement. Except for the LIMIT clause. 

operate

-- 1.使用价格排序(降序)
select * from product order by price desc;
-- 2.在价格排序(降序)的基础上,以分类排序(降序)
select * from product order by price desc,category_id asc;
-- 3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;

aggregation query

Introduction

The queries we have done before are all horizontal queries, which are judged line by line according to the conditions, while the query using the aggregate function is a vertical query, which calculates the value of a column and then returns a single value; in addition, the aggregate function Null values ​​are ignored.

operate

-- 1 查询商品的总条数
select count(*) from product;
-- 2 查询价格大于200商品的总条数
select count(*) from product where price > 200;
-- 3 查询分类为'c001'的所有商品的总和
select sum(price) from product where category_id = 'c001';
-- 4 查询商品的最大价格
select max(price) from product;
-- 5 查询商品的最小价格
select min(price) from product;
-- 6 查询分类为'c002'所有商品的平均价格
select avg(price) from product where category_id = 'c002';

 Aggregate query-handling of NULL values

1. The count function handles null values

If the parameter of the count function is an asterisk ( * ), it will count the number of all records. And if the parameter is a certain field, the number of records containing null values ​​will not be counted.

2. The sum and avg functions handle null values

These two functions ignore the presence of null values, as if the record does not exist. The number is not counted when calculating the average. If default=0 is assumed when creating, it is fine.

3. The max and min functions handle null values

 The max and min functions also ignore the existence of null values.

operate

-- 创建表
create table test_null( 
 c1 varchar(20), 
 c2 int 
);

-- 插入数据
insert into test_null values('aaa',3);
insert into test_null values('bbb',3);
insert into test_null values('ccc',null);
insert into test_null values('ddd',6);
 
-- 测试
select count(*), count(1), count(c2) from test_null;
select sum(c2),max(c2),min(c2),avg(c2) from test_null;

Group query (very important)

Group query refers to the use of group by clauses to group query information.

Format

select field 1, field 2... from table name group by group field having group condition;

-- 分组查询
-- select 字段1,字段2...from 表名 group by 分组字段 having 分组条件;
-- 统计各个分类商品的个数,分组之后select后面只能写分组字段和聚合函数
select catagory_id,count(*) from product group by catagory_id;

Conditional filtering after grouping -having

If you filter the statistical results after grouping , you must use having instead of where
The where clause is used to filter the rows produced by the operation specified in the FROM clause
The group by  clause is used to group the output of the WHERE clause.
The having clause is used to filter rows from the grouped results
Format

select field 1, field 2... from table name group by group field having group condition;

operate

-- 2.统计各个分类商品的个数,且只显示个数大于4的信息
select category_id ,count(*) from product group by category_id having count(*) > 4;
顺序(from,where,group by,having)

Pagination query limit

Introduction

Paged query is common in project development. Due to the large amount of data and the limited length of the display screen, the data needs to be displayed in pages. For example, there are 30 data items, 5 items are displayed on each page, 1-5 items are displayed on the first page, and 6-10 items are displayed on the second page. 

Format

-- Method 1 - display the first n items

select field 1, field 2... from indicates limit n

-- Mode 2-Paging display

select field 1, field 2... from indicates limit m,n

m: Integer, indicating which index to start from, calculation method (current page - 1) * number of items displayed on each page

n: integer, indicating how many pieces of data to query

operate

-- 查询product表的前5条记录 
select * from product limit 5 

-- 从第4条开始显示,显示5条 
select * from product limit 3,5
INSERT INTO SELECT statement
Format

insert into Table2(field1,field2,…) select value1,value2,… from Table1 或者:

insert into Table2 select * from Table1

SELECT INTO FROM statement

Introduction

To import data from one table into another table, there are two options: SELECT INTO and INSERT INTO SELECT

Format

SELECT vale1, value2 into Table2 from Table1

It is required that the target table Table2 does not exist, because the table Table2 will be automatically created when inserting, and the specified field data in Table1 will be copied to Table2.

Appendix full code

create database if not exists mydb2;
use mydb2;
create table product(
pid int primary key auto_increment,
pname varchar(20) not null,
price double,
catagory_id varchar(20)
);

insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲',5000,'c001');

insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲爆休闲裤',266,'c002');
insert into product values(null,'海澜之家唯一',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');

insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');

insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);

-- --简单查询
-- --1查询所有商品

select  pid,pname ,price,catagory_id from product;
-- 或者select * from product
-- 

-- 2查询商品名和商品价格
select pname,price from product;

-- 3.1 表别名
select * from product as p;
-- 或者select * from product p;
-- select p.id,u.id from product,user;
-- 3.2 列别名
select pname as '商品名',price '商品价格' from product;
-- 4去掉重复值
select distinct price from product;
-- 或者select distinct * from product;
-- 5查询结果是表达式(运算查询):将所有商品加价十元后显示
select pname,price+10 new_price from product;

-- 运算符
use mydb2;
-- 1算数运算符
select 6 + 2;
select 6 - 2;
select 6 / 2;
select 6 % 2;

-- 将所有商品加10 元
select pname,price+10 as new_price from product;
-- 将所有商品上调10%
select pname,price * 1.1 as new_price from product;
-- 2 比较运算符合
 
 -- 3 逻辑运算符
 -- 查询商品名称为海尔洗衣机的所有商品
 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;
select * from product where not (price=800);
-- 查询商品价格大于60元的所有商品信息
select * from product where price>60;
-- 查询商品价格在200到1000之间所有商品
select * from product where price >= 200 and price <= 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);
-- 查询含有‘裤'字的所有商品
select * from product where pname like '%裤%';
-- 查询以'海'开头的所有商品
select * from product where pname like '海%';-- 百分号表示任意字符
-- 查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%';
-- 查询category_id为null的商品
select * from product where catagory_id is null;
-- 查询category_id不为null分类的商品
select * from product where catagory_id is not null;
-- 使用least求最小值
select least (10,5,20) as small_number;
select least (10,null,20) as small_number;-- 如果求最小、大值时有null,则无法比较,结果之间为null
-- 使用greatest求最大值

 select greatest (10,5,20) as max_number;
 -- 4位运算符
-- 使用价格排序(降序)

select * from product order by price desc;
-- 2在价格降序的基础上,以分类降序
select * from product order by price desc,catagory_id desc;
-- 3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;

-- 聚合查询

-- 1 查询商品的总条数
select count(pid) from product;
select count(*) from product;
-- 2 查询价格大于200商品的总条数
select count(pid) from product where price > 200;
-- 3 查询分类为'c001'的所有商品的总和
select 
-- 4 查询商品的最大价格
select sum(price) from product where price > 200;
-- 5 查询商品的最小价格
select max(price) from product;
select max(price) max_price, min(price) min_price from product;
-- 6 查询分类为'c002'所有商品的平均价格

select avg(price) from product where catagory_id ='c002';



-- 分组查询
-- select 字段1,字段2...from 表名 group by 分组字段 having 分组条件;
-- 统计各个分类商品的个数,分组之后select后面只能写分组字段和聚合函数
select catagory_id,count(*) from product group by catagory_id;


-- 分页查询
-- 1.查询product表的前五条记录
select * from product limit 5;
-- 2从第四天开始显示,显示五条
 select * from product limit 3,5;-- 从第四天开始显示,显示五条

Guess you like

Origin blog.csdn.net/weixin_44734502/article/details/126267780