如何选择多个最大值,这些值在 MYSQL 中也会是重复值?
为此,请使用联接概念。我们首先创建一个 -
mysql> create table DemoTable1389 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentMarks int -> ); Query OK, 0 rows affected (2.73 sec)
使用 insert 命令在表中插入一些记录。在此,我们还插入了重复值 -
mysql> insert into DemoTable1389(StudentMarks) values(40); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1389(StudentMarks) values(40); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1389(StudentMarks) values(68); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1389(StudentMarks) values(78); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable1389(StudentMarks) values(97); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1389(StudentMarks) values(97); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1389(StudentMarks) values(97); Query OK, 1 row affected (0.49 sec)
使用 select 语句从表中显示所有记录 -
mysql> select * from DemoTable1389;
这将产生以下输出 -
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 1 | 40 | | 2 | 40 | | 3 | 68 | | 4 | 78 | | 5 | 97 | | 6 | 97 | | 7 | 97 | +-----------+--------------+ 7 rows in set (0.00 sec)
以下是用于选择多个最大值的查询 -
mysql> select tbl.StudentId,tbl.StudentMarks from DemoTable1389 tbl -> join ( select max(StudentMarks) as MaxMarks from DemoTable1389) tbl1 -> on tbl1.MaxMarks=tbl.StudentMarks;
这将产生以下输出,其中显示了最大值,以及重复值 -
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 5 | 97 | | 6 | 97 | | 7 | 97 | +-----------+--------------+ 3 rows in set (0.00 sec)
广告