Mysql對字符串進行排序的方法示例:
利用ORDER BY關鍵字進行排序。
語法:
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
示例:
假設有一個Websites表數據如下:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN |
| 3 | 微博 | http://weibo.com/ | 20 | CN |
| 4 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
進行排序:
SELECT * FROM Websites ORDER BY alexa asc;//ASC為升序,DESC為降序,默認是升序
結果:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
+----+--------------+---------------------------+-------+---------+