当前位置:网站首页>[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
边栏推荐
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- 不要使用短路逻辑编写 stl sorter 多条件比较
- 一个bug肝一周...忍不住提了issue
- Visual Studio 2010 configuring and using qt5.6.3
- PostgreSQL 存储结构浅析
- Innovation and upgrading of supply chain system driven management mode in petrochemical industry and strengthening internal management of enterprises
- What useful supplier management systems are available
- Xinchuang operating system -- kylin kylin desktop operating system (project 10 security center)
- 大神详解开源 BUFF 增益攻略丨直播讲座
- Opengauss kernel: analysis of SQL parsing process
猜你喜欢

VS2013 帮助文档中没有 win32/com

北京有哪些牛逼的中小型公司?

抖音实战~我关注的博主列表、关注、取关

MongoDB 在腾讯零售优码中的应用

Qt5.5.1配置MSVC2010编绎器和windbg调试器

wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module

openGauss内核:SQL解析过程分析

【LeetCode】13、罗马数字转整数

5 minutes to make a bouncing ball game

Fleet | background Discovery issue 3: Status Management
随机推荐
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
What! 一条命令搞定监控?
MIPS assembly language learning-03-cycle
大神详解开源 BUFF 增益攻略丨直播讲座
How can the digital intelligent supply chain management platform of the smart Park optimize process management and drive the development of the park to increase speed and quality?
VC2010 编绎Qt5.6.3 提示 CVTRES : fatal error CVT1107:
数组中的第K大元素[堆排 + 建堆的实际时间复杂度]
PostgreSQL enables grouping statistics by year, month, day, week, hour, minute and second
看界面控件DevExpress WinForms如何创建一个虚拟键盘
10: 00 interview, came out at 10:02, the question is really too
ROS knowledge points - build an ROS development environment using vscode
Today's sleep quality record is 80 points
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
零钱兑换(动态规划)
Classic model transformer
tablestore中可以使用sql查询可以查出表中所有的数据吗?
leetcode:22. 括号生成
Fleet |「後臺探秘」第 3 期:狀態管理
物联网云融合安全指南
机器学习之深度学习卷积神经网络,实现基于CNN网络的手写字体识别