如果初始行是随机的,其余的按MySQL的标准排序,该如何选择行呢?
为此,您可以使用ORDER BY CASE语句。让我们创建一个表-
mysql> create table DemoTable1926(
Position varchar(20),
Number int
);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1926 values('Highest',50);mysql> insert into DemoTable1926 values('Highest',30);
mysql> insert into DemoTable1926 values('Lowest',100);
mysql> insert into DemoTable1926 values('Lowest',120);
mysql> insert into DemoTable1926 values('Lowest',90);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1926;
这将产生以下输出-
+----------+--------+| Position | Number |
+----------+--------+
| Highest | 50 |
| Highest | 30 |
| Lowest | 100 |
| Lowest | 120 |
| Lowest | 90 |
+----------+--------+
5 rows in set (0.00 sec)
如果初始行是随机的并且按条件按其余顺序排序,这是选择行的查询-
mysql> select * from DemoTable1926order by Position desc,case Position when 'Highest' then rand() else Number end asc;
这将产生以下输出-
+----------+--------+| Position | Number |
+----------+--------+
| Lowest | 90 |
| Lowest | 100 |
| Lowest | 120 |
| Highest | 50 |
| Highest | 30 |
+----------+--------+
5 rows in set (0.00 sec)
以上是 如果初始行是随机的,其余的按MySQL的标准排序,该如何选择行呢? 的全部内容, 来源链接: utcz.com/z/340989.html