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


我们首先创建一个表 -

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)

更新时间: 2019 年 9 月 27 日

64 次浏览

开启你的事业

通过完成课程获得认证

开始
广告
© . All rights reserved.