亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

MySQL ORDER BY語句介紹

標簽:
MySQL

In this tutorial, you will learn about various natural sorting techniques in MySQL by using the  ORDER BY clause.

Let’s start the tutorial with sample data.

Suppose we have a table named items that contains two columns: id and item_no. To create items table we use the CREATE TABLE statement as follows:

CREATE TABLE if not exists items( id INT AUTO_INCREMENT PRIMARY KEY, item_no VARCHAR(255) NOT NULL );

We use the INSERT statement to insert some data into the items table:

INSERT INTO items(item_no) VALUES ('1'), ('1C'), ('10Z'), ('2A'), ('2'), ('3C'), ('20D');

When we select data and sort it by item_no, we get the following result:

SELECT item_no FROM items ORDER BY item_no;

items table

This is not what we expected. We expect to see the result like the following:

MySQL Natural Sorting - Items table sorted

This is called natural sorting. Unfortunately, MySQL does not provide any built-in natural sorting syntax or function. The ORDER BY clause sorts strings in a linear fashion i.e., one character a time, starting from the first character.

To overcome this, first we split the item_no column into 2 columns: prefix and suffix. The prefix column stores the number part of the  item_no and suffix column stores the alphabetical part. Then, we can sort the data based on these columns as the following query:

SELECT CONCAT(prefix,suffix) FROM items ORDER BY prefix, suffix

The query sorts data numerically first and sort the data alphabetically then. We get the expected result.

The disadvantage of this solution is that we have to break the  item_no into two parts before you insert or update it. In addition, we have to combine two columns into one when we select the data.

If the  item_no data is in fairly standard format, you can use the following query to perform natural sorting without changing the table structure.

SELECT item_no FROM items ORDER BY CAST(item_no AS  UNSIGNED), item_no;

In this query, first we convert  item_no data into unsigned integer by using the CAST function. Second, we use the  ORDER BY clause to sort the rows numerically first and alphabetically then.

Let’s take a look at other common set of data that we often have to deal with.

TRUNCATE TABLE items; INSERT INTO items(item_no) VALUES('A-1'), ('A-2'), ('A-3'), ('A-4'), ('A-5'), ('A-10'), ('A-11'), ('A-20'), ('A-30');

The expected result after sorting is as follows:

items table natural sorting

To achieve this result, we can use the LENGTH function. Notice that LENGTH function returns the length of a string. The idea is to sort the  item_no data by length first and then by column value as the following query:

SELECT item_no FROM items ORDER BY LENGTH(item_no),          item_no;

As you see the data is sorted naturally.

In case all above solutions didn’t work in your situation. You need to perform natural sorting in the application layer. Some languages support natural sorting function e.g., PHP provides natsort() function that sorts an array using natural sorting algorithm.

In this tutorial, we have shown you several techniques to perform natural sorting in MySQL.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/tips/mysql-natural-sorting

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消