来自不同表的SQL最大值

时间:2014-02-05 14:47:28

标签: sql sqlite

我有几个表有一个字段“version”。我想一次从所有表中选择版本的MAX值。 因此,如果t1.version = 1,t2.version = 6,t3.version = 4,则返回值应为“6”。

我试过这个,但看起来错误的做法:

 SELECT MAX(t1.version, t2.version, t3.version) as maxv FROM t1, t2, t3;

在单个查询中以任何方式执行此操作? 问候。

2 个答案:

答案 0 :(得分:2)

尝试这种方式:

select max(version)
from
(
  select version from t1
  union
  select version from t2
  union
  select version from t3
) T

答案 1 :(得分:1)

select max(version)
from
(
  select Max(version) as version from t1
  union
  select Max(version) as version from t2
  union
  select Max(version) as version from t3
) a