我想从postgresql中的不同表中检索多个MAX()值,比如
SELECT MAX(t1.id) AS t1max, MAX(t2.price) AS t2price FROM table1 t1, table2 t2
输出应该看起来像
+-----------------------+
| t1max | t2price | ... |
+-----------------------+
| 100 | 20 | .. |
只有一排。上面的版本有效,但是添加更多表格时速度很慢(我认为是由于交叉连接)。有没有快速的方法来实现这一目标?
答案 0 :(得分:2)
仅加入结果,而不是整个表:
select t1.t1max, t2.t2max
from
(select max(id) t1max from table1) t1,
(select max(price) t2max from table2) t2
答案 1 :(得分:1)
您可以使用子选择执行此操作:
SELECT (SELECT MAX(t1.id) FROM table1 t1) AS t1max,
(SELECT MAX(t2.price) FROM table2 t2) AS t2price
<强> Example on SQL Fiddle 强>