修复特定的列值并在MySQL中为其余行显示随机值

对于随机行,可以使用RAND(),而要修复特定的列,请使用ORDER BY子句。让我们创建一个表-

mysql> create table DemoTable1921
   (
   Number int
   );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1921 values(40);
mysql> insert into DemoTable1921 values(80);
mysql> insert into DemoTable1921 values(820);
mysql> insert into DemoTable1921 values(10);

使用select语句显示表中的所有记录-

mysql> select * from DemoTable1921;

这将产生以下输出-

+--------+
| Number |
+--------+
|     40 |
|     80 |
|    820 |
|     10 |
+--------+
4 rows in set (0.00 sec)

这是修复特定列值并显示具有随机值的其余行的查询-

mysql> select * from DemoTable1921
   order by (Number=40)desc,rand();

这将产生以下输出-

+--------+
| Number |
+--------+
|     40 |
|    820 |
|     10 |
|     80 |
+--------+
4 rows in set (0.00 sec)