选择聚合函数和MySQL中的所有其他列
让我们首先创建一个表-
mysql> create table DemoTable1621 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentMarks int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1621(StudentName,StudentMarks) values('Chris',45); mysql> insert into DemoTable1621(StudentName,StudentMarks) values('Bob',78); mysql> insert into DemoTable1621(StudentName,StudentMarks) values('David',89); mysql> insert into DemoTable1621(StudentName,StudentMarks) values('Adam',87);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1621;
这将产生以下输出-
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 1 | Chris | 45 | | 2 | Bob | 78 | | 3 | David | 89 | | 4 | Adam | 87 | +-----------+-------------+--------------+ 4 rows in set (0.00 sec)
以下是选择聚合函数的查询-
mysql> select tblData.*,( select max(StudentMarks) from DemoTable1621) as MaximumMarks from DemoTable1621 tblData;
这将产生以下输出-
+-----------+-------------+--------------+--------------+ | StudentId | StudentName | StudentMarks | MaximumMarks | +-----------+-------------+--------------+--------------+ | 1 | Chris | 45 | 89 | | 2 | Bob | 78 | 89 | | 3 | David | 89 | 89 | | 4 | Adam | 87 | 89 | +-----------+-------------+--------------+--------------+ 4 rows in set (0.00 sec)