当前位置:网站首页>MySQL advanced (Advanced) SQL statement (I)
MySQL advanced (Advanced) SQL statement (I)
2022-07-29 07:12:00 【m0_ sixty-seven million four hundred thousand nine hundred and 】
Catalog
- Environmental Science : Create table
- One 、order by sentence
- Two 、 Ascending and descending (ASC and DESC)
- 3、 ... and 、select Inquire about
- Four 、and/or- And / or
- 5、 ... and 、where
- 6、 ... and 、distinct( Query does not duplicate records )
- 7、 ... and 、group by( Group results )
- 8、 ... and 、limit
- Nine 、alias( Set alias )
Environmental Science : Create table
mysql> show databases;
mysql> use kgc
mysql> show tables;
mysql> create table ky19;
mysql> create table ky19 (id int,name varchar(10)primary key not null,score decimal(5,2),address varchar(20),hobbid int(5));
mysql> insert into ky19 values(1,'zhusang',100,'riben',2);
mysql> insert into ky19 values(2,'liangsang',59.89,'taiguo',2);
mysql> insert into ky19 values(3,'wusang',80,'miandian',3);
mysql> insert into ky19 values(4,'chenbao',99,'ouzhou',4);
mysql> insert into ky19 values(5,'gangzi',88,'hanguo',5);
mysql> insert into ky19 values(6,'xiangxi',66,'shanghai',3);
mysql> insert into ky19 values(7,'jianjian',87,'cv',6);
mysql> select * from ky19;


One 、order by sentence
( increase 、 Delete 、 Change 、 check )
Yes MysQL Database query , In addition to basic queries , Sometimes you need to process the result set of the query . For example, take only 10 Data 、 Sort or group the query results, etc .
Sort by keyword
PS: Analogy to windows Task manager , Use SELECT Statement can transfer the required data from MySQL From the database , If you sort the results of the query , have access to ORDER BY Statement to sort statements , And finally return the sorted results to the user . The sorting of this statement can not only be for a certain field , You can also target multiple fields
grammar
SELECT column1,column2,... FROM table_name ORDER BY column1,column2,...
---- ORDER BY ---- Sort by keyword
grammar :SELECT " Field " FROM " Table name " [WHERE " Conditions "] ORDER BY " Field " [ASC, DESC];
#ASC It's sorted in ascending order , Is the default sort method .
#DESC It's sorted in descending order .
Two 、 Ascending and descending (ASC and DESC)
ASC:
It's sorted in ascending order , Is the default sort method , namely ASC It can be omitted .SELECT If no specific sorting method is specified in the statement , By default ASC Sort by .
DESC:
Is arranged in descending order . Of course ORDER BY You can also use WHERE Clause to further filter the query results .
3、 ... and 、select Inquire about
---- SELECT ---- Display all data of one or more fields in the table
grammar :SELECT " Field " FROM " Table name ";
SELECT Store_Name FROM Store_Info;




Four 、and/or- And / or
---- AND OR ---- And or
grammar :SELECT " Field " FROM " Table name " WHERE " Conditions 1" {[AND|OR] " Conditions 2"}+ ;
SELECT Store_Name FROM Store_Info WHERE Sales > 1000 OR (Sales < 500 AND Sales > 200);
5、 ... and 、where
--- WHERE ---- Conditional query
grammar :SELECT " Field " FROM " Table name " WHERE " Conditions ";

6、 ... and 、distinct( Query does not duplicate records )
---- DISTINCT ---- Don't show duplicate data
grammar :SELECT DISTINCT " Field " FROM " Table name ";
SELECT DISTINCT Store_Name FROM Store_Info;


Define two fields name,hobbid With name As counting statistics , In the query table score Greater than or equal to 80 In the process of hobbid grouping 
7、 ... and 、group by( Group results )
adopt SQL The result of the search , You can also group the total , Use GROUP BY Statement to implement ,GROUP BY Usually used in conjunction with aggregate functions , Common aggregation functions include : Count ( COUNT) 、 Sum up (SUM)、 Average (AVG)、 Maximum (MAX)、 minimum value (MIN),GROUP BY When grouping, the results can be grouped according to one or more fields .
grammar
SELECT column_name,aggregate_function (column_name)FROM table_name WHERE column_nameoperator valueGROUP BY column name;
---- GROUP BY ---- Yes GROUP BY Summarize and group the query results in the following fields , Usually used in combination with aggregate functions
GROUP BY There is a principle , Namely SELECT In all the following columns , Columns that do not use aggregate functions , Must appear in GROUP BY Back .
grammar :SELECT " Field 1", SUM(" Field 2") FROM " Table name " GROUP BY " Field 1";


8、 ... and 、limit
Limit result entries ( limit )
limit Limit the result record of output
In the use of MySQL SELECT Statement to query , The result set returns all matching records ( That's ok ). Sometimes you just need to return the first line or the first few lines , And that's where it comes in LIMIT Clause .
grammar
SELECT column1,column2,... FROM table_name LIMIT [offset, ] number
LIMIT The first parameter is the position offset ( Optional parameters ), It's settings MysQL Which line to start with . If the first parameter is not set , It will start from the first record in the table . It should be noted that , The position offset of the first record is 0, The second is 1, And so on . second : The first parameter is to set the maximum number of returned record rows .


Nine 、alias( Set alias )
Set alias ( alias ——》as )
stay MysQL When inquiring , When the name of the table is long or some fields in the table are long , For the convenience of writing or
Use the same table multiple times ," You can set aliases for fields, columns, or tables . Use aliases directly when using , Simple and clear , enhance readability
grammar
For column aliases :SELECT column_name AS alias_name FROM table_name;
For table aliases :SELECT column_name (s)FROM table_name As alias_name;
In the use of AS after , It can be used alias_name Instead of table_name, among As i The statement is optional .AS Later alias , It is mainly used to provide temporary names for columns or tables in the table , Use in the process of checking , The actual table name or field name in the library will not be changed



边栏推荐
- 最新百亿量化私募名单
- Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started
- Student achievement ranking system based on C language design
- 实现改变一段文字的部分颜色效果
- MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
- Vscode remote debugging PHP solution through remotessh and Xdebug
- Overview of database system
- 【C语言刷LeetCode】67. 二进制求和(E)
- 2D cartoon rendering - advanced skills
- Image noise and matrix inversion
猜你喜欢

Leetcode-592: fraction addition and subtraction

记 - 踩坑-实时数仓开发 - doris/pg/flink

CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比

Student achievement ranking system based on C language design

接口测试实战项目03:执行测试用例

LeetCode 879. 盈利计划

Sword finger offer II 115: reconstruction sequence

VMware16创建虚拟机:无法创建新虚拟机,不具备执行此操作的权限

After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is

Is online legend software testing training really so black hearted? Are they all scams?
随机推荐
HJ37 统计每个月兔子的总数 斐波那契数列
Some tips of vim text editor
Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
Flink实时仓库-DWD层(下单-多张表实现join操作)模板代码
解决CSDN因版权不明而无法发布博客的问题
IDEA找不到Database解决方法
Image noise and matrix inversion
Excel文件读写(创建与解析)
最新百亿量化私募名单
网上传说软件测试培训真的那么黑心吗?都是骗局?
WPF简单登录页面的完成案例
Basic knowledge of MySQL (high frequency interview questions)
Unity发送Post请求给GoLang服务端解析并返回
Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码
gin 路由,参数,输出
ETL为什么经常变成ELT甚至LET?
Improved Pillar with Fine-grained Feature for 3D Object Detection论文笔记
MySQL queries are case sensitive
个人博客系统(附源码)