MySQl database lesson 8-------SQL command query-------main lifeblood

Author's Foreword

 Welcome little cuties to come and learn from my gtiee Qin Boss (qin-laoda) - Gitee.com

——————————————————————————————

Table of contents

Query data
        condition
        Logical Operators
        fuzzy query
        range query in
        judgment empty
UNION
 to sort
polymerization
Grouping: group by

————————————————————————————

author small nonsense

Recently, Xiaocutie may have discovered that the previous part of my blog was repeated. This is what I wrote on purpose. There are three advantages:

1. The little cutie can review it again after seeing it.

2. Cutie can find her own shortcomings according to this part, and can further analyze the sql statement

3. In fact, I also have the habit of reviewing the previous blog, so writing in this way can further consolidate the knowledge of the previous day

SQL addition, deletion, modification and query

        Add, delete, modify data

--增加数据
insert into 表名 (`字段名1`,`字段名2`)value('内容1','内容2');
--删除数据
delete from 表名 where  条件;

--修改数据
update  表名 set `字段名`=内容  where  条件;

Query data

table alias

--方法1
select * from 表名  as 表别名;
--方法2
select * from 表名 表别名;


view a field

--方法1
select 表名.`字段名`from 表名;

--方法2
select `字段名` from 表名;

--方法3(注意一下,如果表取了别名,使用这个方法就必须使用表别名)
select 表别名.`字段名` from 表名   as  表别名 ;

field alias

--方法1
select `字段名` as 字段别名 from 表名;

--方法2
select 表名.`字段名`as 字段别名 from 表名;
--方法3
select  表别名.`字段名` as `字段别名` from 表名 as 表别名;

view the first few lines

select * from 表名 limit 数字;

Deduplication

-- 方法1
select distinct  * from  表名;

-- 方法2
select distinct  `字段名` from 表名;

condition

Note that name represents the field name, table_name represents the table name

comparison operator

-- 等于
select * from table_name where id = 3;
-- 大于
select * from table_name where id = 3;
-- 大于等于
select * from table_name where id >= 3;
-- 小于
select * from table_name where id < 3;
-- 小于等于
select * from table_name where id <= 3;
-- 不等于
select * from table_name where id != 3;
select * from table_name where id <> 3;
Logical Operators
-- 与
select * from table_name where id > 3 and gender = '男';
-- 或
select * from table_name where id > 3 or gender = '男';
and has higher priority than or, but parentheses can be added to give priority
fuzzy query
Fuzzy query must be used with like
-- 下划线 _ 匹配任意一个字符
select * from table_name where name like '周_';
-- % 匹配任意多个字符
select * from table_name where name like '%周';
range query in
-- 取id为1、4、10的人员数据
select * from table_name where id in (1,4,10);
select * from table_name where id=1 or id=4 or id=10;

between   and

-- between 取连续的数据
select * from table_name where id between 6 and 20;
select * from table_name where id >= 6 and id <=20;
-- 不同的数据库,sql中between的边界可能不同,有些是包头包尾,有些是包头去尾,或者是不包头也不
包尾
judgment empty
-- NULL
select * from table_name where name is null;
-- 非空
select * from table_name where name is not null;
priority
-- 当无法判断条件运行的优先时,可以使用小括号
select * from table_name where id > 10 and name is null or gender = '男';
select * from table_name where id > 10 and (name is null or gender = '男');

There are no pictures here, there will be pictures later

union

The query mentioned above is to query the content of a table. What should I do if a colleague searches for multiple tables? This requires the use of nuion.

 In the splicing process, the two tables associated with UNION don't care whether the names of the fields are the same. However, the format and type of the corresponding fields are required to be consistent, and the number of fields must also be consistent .

Simple understanding is:

1. When splicing two tables, the number of spliced ​​fields should be the same

2. When splicing two tables, the data types corresponding to the spliced ​​fields must be the same

int and date cannot be converted to each other, but in mysql

--拼接显示前10行
select `id`,`name` from 表名 
union
select `zi`, `title` from 表名 limit 10;

distinct (deduplication)

In fact, union also has its own deduplication effect, but in some databases, it is necessary to write distinct


select `id`,`name` from 表名
union distinct 
select `id`, `title` from 表名 limit 10

 all : Returns all result sets, including duplicate data.

select `id`,`name` from 表名
union all
select `id`, `title` from 表名 limit 10;

sort    order by

The default is ascending (smallest to largest)

--写法1
select * from city  where order by `id`;
--写法2
select * from city  where order by `id` asc;

sort by id

Descending (largest to smallest)

select * from city  where order by `id` desc;

Multiple fields to sort
select * from city order by `pid`desc, `id`asc;

It means that the field pid is sorted first. After sorting, if there is data with the same pid, the id sorting will be performed. If there is no data with the same pid, the id sorting will not be performed.
 

 polymerization

Statistics
-- 统计总数
select count(*) from table_name;
select count(0) from table_name;
-- 统计id大于3的人数
select count(0) from table_name where id >3;
--计算某个字段的数据量
select conut(`字段名`) from 表名 ;

Careful little cuties will find that count() can fill in *, numbers, and field names. Let me explain them one by one.

count(`field name`): Count this field. If there is a null value in this field, it will not be counted. It is simply understood as only counting the number of non-null data 

count(*): The field values ​​in each piece of data will be judged one by one, which will cause a lot of performance waste, and the number of returned data

count (number): It will not traverse and judge the fields in each piece of data one by one, as long as every piece of data written in the data table will be counted, so it is recommended to use conut (number) instead of conut (*),

Note that the numbers in conut(number) are arbitrary

maximum value

-- 最大值
select max(id) from table_name;
-- 性别为女的最大ID
select max(id) from table_name where gender='女';

 

minimum value

-- 最小值
select min(id) from table_name;
-- 性别为男的最小ID
select min(id) from table_name where gender='男';

 

sum sum()

-- 求和
select sum(age) from table_name;
-- 性别为男的年龄总值
select sum(age) from table_name where gender='男';

Remember that count and sum are not the same, count is the calculation of quantity, sum is the calculation of sum

 

average avg()

-- 平均值
select avg(age) from table_name;
-- 性别为男的年龄平均值
select avg(age) from table_name where gender='男';
select sum(age)/count(0) from table_name where gender='男';

 The average can also be written like this

select sum(`字段名`)/count(`字段名`) from 表名 ;

 Grouping: group by

Note, which field is grouped, you can only view that field, when the written field does not participate in the grouping, an error will be reported

The query results are grouped by fields, and those with the same field values ​​form a group. Can be used for single field grouping or multiple field grouping

A simple understanding is that the same is a group, for example, there are many people in a class. Group by interests, the same hobbies as a group,

-- 性别分组
select gender from table_name group by gender;
-- 利用分组去重
select id, name from table_name group by id, name;
-- 这里的去重是利用group by进行去重,它的效率会比distinct快,但是要求将进行去重的字段全部写入
--分组内

 

Using grouping will also produce a deduplication effect,

After calculating the grouping, the number of people in each group

select `字段名`,count(0)from 表名 group by `字段名`;

If you want to add the where judgment condition, you must write it in front of the group by 

 select * from city where `id`> 3 order by `id`;
Word end splicing after grouping
-- 分组后的字段拼接
select gender, group_concat(name) from table_name group by gender;
select gender, concat(name) from table_name group by gender;

 The so-called field splicing means that after grouping, we can understand it as collecting certain characteristics of the group members

select `pid` ,group_concat(`name`),count(0) from(select * from city limit 4)as a  group by `pid` order by `pid`;

 The meaning in the brackets is to get the first four pieces of data in the city table, alias it as a, and then group the pid field of the a table, and then sort in ascending order and splicing the name field,

-- 分组后的聚合
-- 各个性别的人数
select gender, count(0) from table_name group by gender;
-- 各个性别的平均年龄
select gender, avg(age) from table_name group by gender;

with rollup       coalesce()

partial summation

-- 使用coalesce代替空值
select coalesce(gender, 'total'), count(0) num from table_name group by gender -
- with rollup;

with rollup is used for partial aggregation, which can count the number of values ​​of the same field and name it with null

coalesce (field name, 'name') changes the empty value in the field name to name

 Results filtering having

-- 分组后的条件筛选
-- 各个性别的平均年龄大于10的数据
select gender, avg(age) from table_name group by gender having avg(age) >10;
-- 各个性别的平均年龄大于10的人数
select gender, count(0) from table_name group by gender having avg(age) >10;

where is to filter the initial value

having is to filter the result value

 Summarize

If the where condition is written, it must be written before the results are filtered out, such as before the group by, because where is the initial value for filtering, and writing where after is equivalent to filtering the results.

Guess you like

Origin blog.csdn.net/m0_69984273/article/details/131732924