时间:2022-12-06 01:09
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 |
+----+--------------+---------------------------+-------+---------+