让我们首先创建一个表-
mysql> create table DemoTable1468 -> ( -> Id int, -> Name varchar(20), -> Age int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1468 values(100,'Chris',23); mysql> insert into DemoTable1468 values(101,'Bob',25); mysql> insert into DemoTable1468 values(102,'David',30); mysql> insert into DemoTable1468 values(100,'Chris',23); mysql> insert into DemoTable1468 values(100,'Chris',38); mysql> insert into DemoTable1468 values(101,'Bob',23);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1468;
这将产生以下输出-
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 100 | Chris | 23 | | 101 | Bob | 25 | | 102 | David | 30 | | 100 | Chris | 23 | | 100 | Chris | 38 | | 101 | Bob | 23 | +------+-------+------+ 6 rows in set (0.00 sec)
这是删除具有重复内容和相似内容的行并使用select语句获取最大数量的行的查询-
mysql> select Id,Name,Age from DemoTable1468 -> group by Name,Id -> having max(Age);
这将产生以下输出-
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 100 | Chris | 23 | | 101 | Bob | 25 | | 102 | David | 30 | +------+-------+------+ 3 rows in set (0.00 sec)