使用 MySQL 从某一列的值中获取最大值并将其设置为同一列中的所有其他值?
mysqlmysqli database更新于 2025/12/23 18:52:17
首先我们创建一个表 −
mysql> create table DemoTable ( FirstName varchar(100), Score int ); Query OK, 0 rows affected (0.60 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('David',59);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Chris',97);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values('Bob',98);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Carol',91);
Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------+-------+ | FirstName | Score | +-----------+-------+ | David | 59 | | Chris | 97 | | Bob | 98 | | Carol | 91 | +-----------+-------+ 4 rows in set (0.00 sec)
以下查询从列值中获取最大值并将其设置为同一列中的所有其他值 −
mysql> select FirstName,(select max(Score) from DemoTable) as Score from DemoTable;
这将产生以下输出 −
+-----------+-------+ | FirstName | Score | +-----------+-------+ | David | 98 | | Chris | 98 | | Bob | 98 | | Carol | 98 | +-----------+-------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

