从两个 MySQL 表中获取一列中的 MAX() ?
使用 GREATEST() 查找最大值。我们首先创建一个表 -
mysql> create table DemoTable1 ( Number int ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1 values(80); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1 values(229); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(575); Query OK, 1 row affected (0.24 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable1;
输出
+--------+ | Number | +--------+ | 80 | | 229 | | 575 | +--------+ 3 rows in set (0.00 sec)
以下是创建第二个表的查询 -
mysql> create table DemoTable2 ( Number int ); Query OK, 0 rows affected (0.51 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable2 values(485); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2 values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2 values(475); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable2;
输出
+--------+ | Number | +--------+ | 485 | | 10 | | 475 | +--------+ 3 rows in set (0.00 sec)
以下是查询两个表中一列的 MAX() 的查询 -
mysql> select greatest((select max(Number) from DemoTable1),(select max(Number) from DemoTable2));
输出
+-----------------------------------------------------------------------------------------+ | greatest((select max(Number) from DemoTable1),(select max(Number) from DemoTable2)) | +-----------------------------------------------------------------------------------------+ | 575 | +-----------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
广告