当前位置:网站首页>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



边栏推荐
- mysql可以定时导出表格吗?
- ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
- Teacher Wu Enda machine learning course notes 01 introduction
- Basic knowledge of MySQL (high frequency interview questions)
- Flink实时仓库-DWD层(流量域)模板代码
- dba
- Implementation of book borrowing management system based on C language
- 使用VsCode配置MySQL实现连接、查询、等功能
- Image noise and matrix inversion
- Unity free element special effect recommendation
猜你喜欢

Flink实时仓库-DWD层(流量域)模板代码

CVPR2022Oral专题系列(一):低光增强

OCR光学字符识别方法汇总

win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案

MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of

DM data guard cluster setup

MySQL 高级(进阶) SQL 语句 (一)

MutationObserver文档学习

做开发4年13K,想转行自动化测试,薪资还能涨吗···

IO stream - file - properties
随机推荐
Revolution of game assets
Vscode remote debugging PHP solution through remotessh and Xdebug
谷歌零碎笔记之JWT(草稿)
MySQL queries are case sensitive
自定义事件
Excel文件读写(创建与解析)
MySQL 高级(进阶) SQL 语句 (一)
IO stream - file - properties
pytorch的技巧记录
win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案
buck电路boot电容短路和断路实测波形
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
SQL优化
DM data guard cluster setup
win11系统错误:由于找不到 iertutil.dll,无法继续执行代码。重新安装程序可能会解决此问题
H3C_利用设置缺省静态路由优先级实现出口双线路的主备功能
Guess the number / / generate a random number for the first time
Idea cannot find a database solution
gin 服务退出
实现改变一段文字的部分颜色效果