Database operation (2) Advanced query (select)

This article uses navicat for experiments. The data table used has 15 rows and 8 columns, including 8 fields of ID, name, address, password, email, mathematics, English, and Chinese.
The complete database table used is as follows, and the related operations on creating the database are in my last blog post:
The complete data sheet used in this article


1. Deduplication (distinct)

insert image description here


Second, sorting (order by)

# 以下三句都是按照英语成绩来排序
# select * from users ORDER BY 7;
# select id,name,mail,english from users ORDER BY english;
select id,name,mail,english from users ORDER BY 4;

insert image description here

# 如果order by后面使用数字,但是该数字的值超出了数据库的列数则产生报错。
#(该原理常用在sql注入中,用来判断数据库列数)
select * from users ORDER BY 9;

insert image description here


3. Joint query (union)

# 在sql注入中我们时常会使用这样的句式“union select 1,2,3”,用来找显示位。
#(前提是已经通过order by 知道列数)
select id,name,password from users union select 1,2,3;

insert image description here

# union正常的作用是将两个查询语句合并在一起,可以是同一个表的,也可以是不同表的数据、
# select id,name,password from users union select Math,english,chinese from users;
# 下面的students表和users表为同一数据库中的表
select * from users union select * from students;

insert image description here


4. Specified range (where)

# where操作符可与 <,>,=,!=,>=,<=,and,or,not 这些运算符结合使用
# 筛选出数学成绩大于等于80分,并且英语成绩小于50分的同学
select * from users where math>=80 and english<50;

insert image description here


5. Fuzzy query (like)

# LIKE(模糊查询)子句通常用在搜索(WHERE)里面。
# %(通配符)表示任何字符出现任意次数,_表示任何字符出现一次(_只能带一个)
select * from users where mail like '%@189.com';

insert image description here


6. Group by

The group by syntax groups the result set of the select query according to a certain field or expression to obtain a set of groups, and then extracts the value of a specified field or expression from each group.

Query the sum of scores for each region:

select address,sum(math) from users group by address;

insert image description here

I also encountered a lot of errors when using the group by statement for the first time, such as:

select address,math from users group by address
> 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.users.math' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

The reason for this error is that the field specified by select in the group by statement must be a "group by field", and other fields must be included in the aggregation function if they want to appear in the select.
For example, in the example, the group by is followed by address. The address is used as the basis for grouping, but if I want to see the content of the score at the same time, I need to put the score in an aggregation function, such as sum(math).

# 常用聚合函数
sum()  max()  min()  avg()  count()
first()  last()  # 仅Access数据库支持

screening (having)

The having clause filters group records after aggregation

Use count for each region

-- 使用count()对所有地区的人员进行分类并计数
select address,count(chinese) num from users group by address;
-- 加上having筛选出总人数大于2的地区
select address,count(chinese) num from users group by address having num>2;

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/zibery/article/details/127202089