用于选择具有最大年龄值的所有用户
我们首先创建一个表 -
mysql> create table DemoTable ( Name varchar(100), Age int ); Query OK, 0 rows affected (0.46 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values('Chris',23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('David',55); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Bob',53); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Mike',54); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Sam',55); Query OK, 1 row affected (0.18 sec)
使用选择语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+-------+------+ | Name | Age | +-------+------+ | Chris | 23 | | David | 55 | | Bob | 53 | | Mike | 54 | | Sam | 55 | +-------+------+ 5 rows in set (0.00 sec)
以下是选择具有最大年龄值的所有用户的查询 -
mysql> select *from DemoTable where Age=(select max(Age) from DemoTable);
这将产生以下输出 -
+-------+------+ | Name | Age | +-------+------+ | David | 55 | | Sam | 55 | +-------+------+ 2 rows in set (0.00 sec)
广告