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 |
+----+--------------+---------------------------+-------+---------+
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: mysql中字符串怎么比较大小