当前位置:网站首页>[MySQL] official website document learning query statement SQL precautions
[MySQL] official website document learning query statement SQL precautions
2022-06-28 16:02:00 【Ch.yang】
Preface
- Test data scripts
CREATE TABLE `class` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `james`.`class` (`id`, `name`) VALUES (1, ' Class one ');
INSERT INTO `james`.`class` (`id`, `name`) VALUES (2, ' Class two ');
INSERT INTO `james`.`class` (`id`, `name`) VALUES (3, ' Class three ');
INSERT INTO `james`.`student` (`id`, `name`, `age`, `score`) VALUES (1, 'james', 18, 60);
INSERT INTO `james`.`student` (`id`, `name`, `age`, `score`) VALUES (2, 'tom', 18, 80);
INSERT INTO `james`.`student` (`id`, `name`, `age`, `score`) VALUES (3, 'jerry', 20, 40);
INSERT INTO `james`.`student` (`id`, `name`, `age`, `score`) VALUES (4, 'candy', 16, 70);
INSERT INTO `james`.`student` (`id`, `name`, `age`, `score`) VALUES (5, 'kobe', 20, 60);
Query statement
Can be disabled group by The default sort of ( Reduce sorting overhead )

original text : If you use GROUP BY, output rows are sorted according to the GROUP BY columns as if you had an ORDER BY for the same columns. To avoid the overhead of sorting that GROUP BY produces, add ORDER BY NULL:
paraphrase : Use group by when , The default will be for group by Column sorting ( As shown in Figure 2 , Press age Positive order ). If you want to cancel this default behavior , You can add
order by null.
select * from student group by age order by null;
stay selete Use in order by group by Don't ignore when sorting max_sort_length
original text : When you use ORDER BY or GROUP BY to sort a column in a SELECT, the server sorts values using only the initial number of bytes indicated by the max_sort_length system variable.
paraphrase : Use order by or group by When sorting , The length used for sorting in the field is less than the global variable max_sort_length Specified number of bytes ( Unit is bytes).
show variables like 'max_sort_length';

group by [ ] with rollup Bring out the statistics

HAVING Precautions for
original text :The HAVING clause, like the WHERE clause, specifies selection conditions. The WHERE clause specifies conditions on columns in the select list, but cannot refer to aggregate functions. The HAVING clause specifies conditions on groups, typically formed by the GROUP BY clause. The query result includes only groups satisfying the HAVING conditions. (If no GROUP BY is present, all rows implicitly form a single aggregate group.)
paraphrase ( It's about understanding ):
- where Is to filter the returned result set , The implication is :where stay select Execute... Before statement execution , If there is group by sentence ,where You can only see the result set after grouping , Unable to read the details of each group before grouping .
- having It is used to filter the grouped result set , The implication is :having You can use aggregate functions to write conditions , You can also write conditions without using aggregate functions ( amount to where The function of , But it will cause ambiguity ).
-- Students are grouped by age , And as long as the average score is greater than 50 Group
select age from student group by age having avg(score) > 50;
- because as Operation in where After performing , The same requirement cannot be implemented with the following statement
select age, avg(score) as b from student group by age where b > 50; // error
- where It is independent of the aggregate function , So you can't use an aggregate function as a statement
select age from student group by age where avg(score) > 50; // error
- having Not with group by Together with , It can be executed , If having Do not use aggregate function after , Semantics and where Agreement , It is suggested to change it to where.
select * from student having score > 50; // wrong
-- Modified into where
select * from student where score > 50;
- having Not with group by Together with , If at this time having Using aggregate functions , Treat all result sets as a group ;

limit Note the semantics of the two parameters
- Before acquisition n rows
select * from student limit 3;
- The offset from is 3 Start of record , Take back 1 Bar record
The offset of the first line is 0, So it's from id=4 Start with
Specify index explicitly index hints
SELECT * FROM table1 USE INDEX (col1_index,col2_index) WHERE col1=1 AND col2=2 AND col3=3;
SELECT * FROM table1 IGNORE INDEX (col3_index) WHERE col1=1 AND col2=2 AND col3=3;
SELECT * FROM t1 USE INDEX (i1) IGNORE INDEX FOR ORDER BY (i2) ORDER BY a;
original text : Index hints apply to SELECT and UPDATE statements. They also work with multi-table DELETE statements, but not with single-table DELETE,
paraphrase :index hints The grammar of can be used in select and update On , If and only if multiple tables participate ( Subquery or connection ) Can only be used in delete On
Do not use column subscripts
- group by / order by You can use aliases in statements , You can even use subscripts , It is worth mentioning that the subscript of the column is from 1 Start , But don't do it
SELECT college, region, seed FROM tournament ORDER BY 2, 3;
original text : Use of column positions is deprecated because the syntax has been removed from the SQL standard.
paraphrase : The use of subscripts has been sql Standard deletion
边栏推荐
- 不要使用短路逻辑编写 stl sorter 多条件比较
- 10: 00 interview, came out at 10:02, the question is really too
- Go zero micro Service Practice Series (VII. How to optimize such a high demand)
- 防火墙基础之流量管理与控制
- Navicat 15 for MySQL
- VS2013 帮助文档中没有 win32/com
- 分布式 CAP 定理的前世今生
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- Jenkins的安装及使用
- What! 一条命令搞定监控?
猜你喜欢

go-zero 微服务实战系列(七、请求量这么高该如何优化)

among us私服搭建

Qt create 5.0.3 配置Qt4.8.7

Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(上)

Big God explains open source buff gain strategy live lecture

Qt5.5.1 configuring msvc2010 compiler and WinDbg debugger

部门新来了个字节25K出来的,让我见识到了什么是天花板

【Proteus仿真】L297驱动步进电机

【推荐系统】多任务学习之ESMM模型(更新ing)

SaaS application management platform solution in the education industry: help enterprises realize the integration of operation and management
随机推荐
Web worker poll request
Analysis of PostgreSQL storage structure
IPDK — Overview
A bug liver a week I can't help mentioning issue
10年测试经验,在35岁的生理年龄面前,一文不值
Etcd可视化工具:Kstone简介(一)
Visual Studio 2010 configuring and using qt5.6.3
10:00面试,10:02就出来了 ,问的实在是太...
关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
如何查询数据库中一个表中的所有数据呢?
The past and present life of distributed cap theorem
Notes to distributed theory
Visual Studio 2010 compilation qt5.6.3
10 years of testing experience, worthless in the face of the physiological age of 35
No win32/com in vs2013 help document
The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
Focus on the 35 year old Kan: fear is because you don't have the ability to match your age
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
Smart supplier management system for chemical manufacturing industry deeply explores the field of supplier management and improves supply chain collaboration
Installation and use of Jenkins