按 MySQL 中列的长度排序
要按 MySQL 中列的长度排序,请使用 ORDER BY LENGTH。
我们先创建一个表 -
mysql> create table DemoTable715 (UserMessage varchar(100)); Query OK, 0 rows affected (0.56 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable715 values('Aw'); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable715 values('Awe'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable715 values('A'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable715 values('Awes'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable715 values('Awesom'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable715 values('Awesome'); Query OK, 1 row affected (0.19 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable715;
将生成以下输出 -
+-------------+ | UserMessage | +-------------+ | Aw | | Awe | | A | | Awes | | Awesom | | Awesome | +-------------+ 6 rows in set (0.00 sec)
以下是按列的长度排序的查询 -
mysql> select *from DemoTable715 order by length(UserMessage);
将生成以下输出 -
+-------------+ | UserMessage | +-------------+ | A | | Aw | | Awe | | Awes | | Awesom | | Awesome | +-------------+ 6 rows in set (0.00 sec)
广告