2个连接表的2列之间的mysql max

时间:2011-11-04 15:10:47

标签: mysql

我有2个表,每个

中有相同的列

我可以创建一个将加入2个表并在2

之间给出最大值的查询

我想出了

SELECT MAX(certificate_number) AS max FROM contacts_cstm
UNION
SELECT MAX(certificate_number) AS max FROM accounts_cstm

但这给了我每个人的最大价值。如何修改此查询以在两者之间给出最大值

由于

3 个答案:

答案 0 :(得分:4)

使用其他查询进行包装可以解决问题:

SELECT MAX(certificate_number) AS max
FROM (
    SELECT certificate_number FROM contacts_cstm
    UNION
    SELECT certificate_number FROM accounts_cstm
) as CHILD

答案 1 :(得分:3)

MAX()之外调用UNION,而不是在内部。

SELECT MAX(maxnum) AS maxnum FROM
(
  SELECT MAX(certificate_number) as maxnum FROM contacts_cstm
  UNION
  SELECT MAX(certificate_number) as maxnum FROM accounts_cstm
) subq

答案 2 :(得分:0)

尝试:

SELECT MAX(m) FROM
    (SELECT MAX(certificate_number) as m FROM contacts_cstm
     UNION
     SELECT MAX(certificate_number) as m FROM accounts_cstm) t

SELECT MAX(certificate_number) FROM
    (SELECT certificate_number FROM contacts_cstm
     UNION
     SELECT certificate_number FROM accounts_cstm) t