首页 文章资讯内容详情

从一列值中获取最大值并将其设置为与MySQL在同一列中的所有其他值?

2026-06-04 1 花语

让我们首先创建一个表-

create table DemoTable ( FirstName varchar(100), Score int );

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

insert into DemoTable values(David,59); insert into DemoTable values(Chris,97); insert into DemoTable values(Bob,98); insert into DemoTable values(Carol,91);

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

select *from DemoTable;

这将产生以下输出-

+-----------+-------+ | FirstName | Score | +-----------+-------+ | David | 59 | | Chris | 97 | | Bob | 98 | | Carol | 91 | +-----------+-------+ 4 rows in set (0.00 sec)

以下是从列值获取最大值并将其设置为同一列中所有其他值的查询-

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)