当前位置:网站首页>100 important knowledge points that SQL must master: sorting and retrieving data
100 important knowledge points that SQL must master: sorting and retrieving data
2022-06-27 21:29:00 【Guge academic】
3.1 Sorting data
As mentioned in the previous lesson , Below SQL Statement returns a single column of a database table . But look at
Its output , There is no specific order
Input ▼
SELECT prod_name
FROM Products;
Output ▼
prod_name
--------------------
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann
King doll
Queen doll
Actually , The retrieved data is not displayed randomly . If you don't sort , Data will generally be represented by it
The order in which they appear in the table shows , This may be the order in which the data was first added to the table . however ,
If the data is subsequently updated or deleted , Then this order will be affected by DBMS Reuse back
The impact of the way storage space is collected . therefore , If there's no clear control , Then the final result
You can't ( Should not be ) Depending on the sort order . Relational database design theory holds that , If you don't
Specify the sort order , The order of retrieved data should not be assumed to have any meaning .
Clause (clause)
SQL A sentence is made up of clauses , Some clauses are necessary , Some are optional . A dime
A sentence usually consists of a keyword plus the data provided . The example of clause is as follows
What I saw in the first lesson SELECT Of the statement FROM Clause .
In order to make clear the order SELECT The data retrieved by the statement , You can use ORDER BY Clause .
ORDER BY Clause takes the name of one or more columns , Sort the output accordingly . Please see the following
Example :
Input ▼
SELECT prod_name
FROM Products
ORDER BY prod_name;
analysis ▼
Except for instructions DBMS Software pair prod_name Columns sort data in alphabetical order ORDER BY
Out of clause , This statement is the same as the previous statement . give the result as follows .
Output ▼
prod_name
--------------------
12 inch teddy bear
18 inch teddy bear
8 inch teddy bear
Bird bean bag toy
Fish bean bag toy
King doll
Queen doll
Rabbit bean bag toy
Raggedy Ann
Be careful : ORDER BY Position of clause
In the specified section ORDER BY When clause , It should be guaranteed that it is SELECT The last word in the sentence
Tiaozi sentence . If it's not the last clause , Will go wrong .
Tips : Sort by non selected columns
Usually , ORDER BY The columns used in the clause will be the columns selected for display . however , real
This is not necessarily the case , Non - Legal columns are used to retrieve non - legal data .
3.2 Sort by multiple columns
It is often necessary to sort data by more than one column . for example , If you want to display the employee list , can
Can you wish to sort by last name and first name ( First, sort by surname , Then sort by first name in each surname ). Such as
If more than one employee has the same last name , This is very useful .
To sort by multiple columns , Just specify these column names , Separate column names with commas ( It's like
When multiple columns are selected ).
The following code searches 3 Columns , And sort the results by two columns —— First, according to the price
grid , Then sort by name .
Input ▼
SELECT prod_id, prod_price, prod_name
FROM Products
ORDER BY prod_price, prod_name;
Output ▼
prod_id prod_price prod_name
------- ---------- --------------------
BNBG02 3.4900 Bird bean bag toy
BNBG01 3.4900 Fish bean bag toy
BNBG03 3.4900 Rabbit bean bag toy
RGAN01 4.9900 Raggedy Ann
BR01 5.9900 8 inch teddy bear
BR02 8.9900 12 inch teddy bear
RYL01 9.4900 King doll
RYL02 9.4900 Queen doll
BR03 11.9900 18 inch teddy bear
It is important to understand that when sorting by multiple columns , The order of sorting is completely in accordance with the regulations . In other words,
say , For the output in the above example , Only if more than one row has the same prod_price When the value of
Just press prod_name Sort . If prod_price All values in the column are
Unique , You don't press prod_name Sort .
3.3 Sort by column position
In addition to being able to indicate the sort order by column name , ORDER BY It also supports sorting by relative column position
order . In order to understand this content , Let's take an example :
Input ▼
SELECT prod_id, prod_price, prod_name
FROM Products
ORDER BY 2, 3;
Output ▼
prod_id prod_price prod_name
------- ---------- --------------------
BNBG02 3.4900 Bird bean bag toy
BNBG01 3.4900 Fish bean bag toy
BNBG03 3.4900 Rabbit bean bag toy
RGAN01 4.9900 Raggedy Ann
BR01 5.9900 8 inch teddy bear
BR02 8.9900 12 inch teddy bear
RYL01 9.4900 King doll
RYL02 9.4900 Queen doll
BR03 11.9900 18 inch teddy bear
analysis ▼
You can see , The output here is the same as the query above , The difference is ORDER BY Son
sentence . SELECT What is specified in the listing is the relative position of the selected column, not the column name . ORDER BY 2
According to the said SELECT The second column in the list prod_price Sort . ORDER BY 2,
3 First press prod_price , Press again prod_name Sort .
The main benefit of this technique is that you don't have to re-enter the column name . But it also has disadvantages . First , No
Giving the column name explicitly may cause misusing the column name to sort . secondly , In the face of SELECT The list is updated
It is easy to sort the data incorrectly ( Forget about ORDER BY Change the clause accordingly ).
Last , If the column to be sorted is not in SELECT In the list , Obviously you can't use this technology .
Tips : Sort by unselected column
obviously , When the basis does not appear in SELECT When sorting columns in the list , This... Cannot be used
technology . however , If necessary , You can mix actual column names with relative column positions .
3.4 Specify sorting direction
Data sorting is not limited to ascending sorting ( from A To Z), This is the default sort order . just so so
Use ORDER BY Clauses in descending order ( from Z To A) Sort . For descending sort ,
Must specify DESC keyword .
The following example sorts products in descending order of price ( The most expensive is at the top ):
Input ▼
SELECT prod_id, prod_price, prod_name
FROM Products
ORDER BY prod_price DESC;
Output ▼
If you plan to sort with multiple columns , What should I do ? Sort the products in descending order ( Most expensive
Of is at the front ), Plus the product name :
Input ▼
SELECT prod_id, prod_price, prod_name
FROM Products
ORDER BY prod_price DESC, prod_name;
Output ▼
prod_id prod_price prod_name
------- ---------- --------------------
BR03 11.9900 18 inch teddy bear
RYL01 9.4900 King doll
RYL02 9.4900 Queen doll
BR02 8.9900 12 inch teddy bear
BR01 5.9900 8 inch teddy bear
RGAN01 4.9900 Raggedy Ann
BNBG02 3.4900 Bird bean bag toy
BNBG01 3.4900 Fish bean bag toy
BNBG03 3.4900 Rabbit bean bag toy
analysis ▼
DESC Keywords only apply to column names directly before them . In the example above , Only right prod_price
Column specifies DESC , Yes prod_name Column does not specify . therefore , prod_price Columns are arranged in descending order
order , and prod_name Column ( Within each price ) Still sort in standard ascending order .
Warning : Sort in descending order on multiple columns
If you want to sort in descending order on multiple columns , You must specify... For each column DESC keyword .
Please note that , DESC yes DESCENDING Abbreviation , Both of these keywords can be used . And DESC
The opposite is ASC ( or ASCENDING ), You can specify it when sorting in ascending order . But actually ,
ASC It's not much use , Because ascending is the default ( If you don't specify ASC It doesn't specify
DESC , Then assume ASC ).
Tips : Case sensitive and sort order
When sorting textual data , A And a Is it the same ? a be located B Before , still Z
after ? These are not theoretical issues , The answer depends on how the database is set up .
In dictionary (dictionary) In the sort order , A Be regarded as being associated with a identical , This is most numbers
According to the default practice of the library management system . however , many DBMS Allow database administrators to
It's time to change this behavior ( If your database contains a large number of foreign language characters , It may have to be
Sample preparation ).
The key question here is , If you really need to change this sort order , With simple ORDER
BY Clause may not be able to do so . You must ask the database administrator for help .
边栏推荐
- VMware vSphere ESXi 7.0安装教程
- Implementation string mystring
- Question brushing notes - tree (easy) - updating
- Share how I take notes
- 展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
- 动物养殖生产虚拟仿真教学系统|华锐互动
- Ceph分布式存储
- Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
- 安装gatewayworker之后启动start.php
- MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)
猜你喜欢
送你12个常用函数公式,用过的都说好
Can Oracle's CTAs bring constraints and other attributes to the new table?
Very comprehensive dolphin scheduler installation and use documents
308. 2D area and retrieval - variable segment tree / hash
[STL programming] [common competition] [Part 2]
Focus! Tips for installing fonts on domestic computers
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
mysql使用笔记一
2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
Necessary software tools in embedded software development
随机推荐
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
分享|智慧环保-生态文明信息化解决方案(附PDF)
No wonder people chose apifox instead of postman
oss上传调用的是哪个方法
实际工作中用到的shell命令 - sed
灵活的IP网络测试工具——— X-Launch
Very comprehensive dolphin scheduler installation and use documents
麒麟V10安装字体
UE4 essays: fstring, fname and ftext
Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!
Kirin V10 installation font
Use the storcli tool to configure raid. Just collect this article
BTC和ETH重新夺回失地!引领市场复苏?加密将步入“冰河时代”!
Focus! Tips for installing fonts on domestic computers
Is it safe to open an account and buy stocks? Who knows
爱数课实验 | 第七期-基于随机森林的金融危机分析
Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
JPA踩坑系列之save方法
众昂矿业:新能源或成萤石最大应用领域