我有一个类似这样的查询(不,我的表和字段不是由这些名称调用的,但是结构是相同的)-
SELECT table1.id, table2.id, table1.phone1
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone1 IS NOT NULL AND LTRIM(RTRIM(table1.phone1)) <> ''
UNION
SELECT table1.id, table2.id, table1.phone2
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone2 IS NOT NULL AND LTRIM(RTRIM(table1.phone2)) <> ''
UNION
SELECT table1.id, table2.id, table2.phone
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table2.phone IS NOT NULL AND LTRIM(RTRIM(table2.phone)) <> ''
运行良好。但是,当我试图从中进行选择时(我最终将尝试使用group bys等操作,但目前只尝试了一个简单的选择),如下所示:
SELECT * FROM
( SELECT table1.id, table2.id, table1.phone1
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone1 IS NOT NULL AND LTRIM(RTRIM(table1.phone1)) <> ''
UNION
SELECT table1.id, table2.id, table1.phone2
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table1.phone2 IS NOT NULL AND LTRIM(RTRIM(table1.phone2)) <> ''
UNION
SELECT table1.id, table2.id, table2.phone
FROM table1 LEFT OUTER JOIN
table3 ON table3.id = table1.id LEFT OUTER JOIN
table2 ON table3.id2 = table2.id
WHERE table2.phone IS NOT NULL AND LTRIM(RTRIM(table2.phone)) <> '' )
我知道错误:
Incorrect syntax near ')'.
我做错什么了?