我有以下UNION
'声明:
SELECT 8
UNION ALL
SELECT 7
UNION ALL
SELECT 10
...和正常的SELECT
声明:
SELECT column1
FROM table1
现在我的问题是,如何合并两者并得到以下结果
SELECT column1, 10 -- 10 is the biggest element in the UNION statement
FROM table1
答案 0 :(得分:4)
为了回归笛卡尔产品,没有什么特别漂亮:
SELECT t1.column1,
(SELECT MAX(x.col)
FROM (SELECT 8 AS col
UNION ALL
SELECT 7 AS col
UNION ALL
SELECT 10 AS col) x)
FROM TABLE1 t1
ANSI-89方法:
SELECT t1.column1,
y.max_col
FROM TABLE1 t1,
(SELECT MAX(x.col) AS max_col
FROM (SELECT 8 AS col
UNION ALL
SELECT 7 AS col
UNION ALL
SELECT 10 AS col) x) y
ANSI-92方法,使用CROSS JOIN(并非所有数据库都支持):
SELECT t1.column1,
y.max_col
FROM TABLE1 t1
CROSS JOIN (SELECT MAX(x.col) AS max_col
FROM (SELECT 8 AS col
UNION ALL
SELECT 7 AS col
UNION ALL
SELECT 10 AS col) x) y
答案 1 :(得分:1)
怎么样?
SELECT column1, (SELECT MAX(v) FROM (SELECT 8 AS v
UNION ALL
SELECT 7 AS v
UNION ALL
SELECT 10 AS v
) x)
FROM table1
答案 2 :(得分:1)
SELECT column1, (SELECT MAX(TABLE2.X)
FROM (SELECT 8 AS X UNION ALL SELECT 7 AS X UNION ALL SELECT 10 AS X) TABLE2 )
AS MY_MAX
FROM TABLE1