UNION JOIN和MS-ACCESS

时间:2011-07-21 17:05:27

标签: ms-access

真正令人困惑的是,它在喷气机下工作,但不能在交通工具下工作....

如何进行内部联接WITH union

SELECT field1, field2 FROM
(SELECT field1, field2, field3 FROM table1
JOIN table3 ON table1.id=table3.id
UNION ALL
SELECT field1, field2, field3 FROM table2 
JOIN table4 ON table2.id=table4.id) 
WHERE field3=1

结果:

  

3131 - “FROM子句中的语法错误”

我把它缩小为:

SELECT field1 FROM
(SELECT field1, field3 FROM table1
UNION ALL 
SELECT field1, field3 FROM table2)
WHERE field3=1

但仍无法正常工作

即便如此:

SELECT * FROM
(SELECT field1 FROM table1 
UNION ALL
SELECT field1 FROM table2)

不起作用

SELECT field1 FROM table1 UNION ALL SELECT field1 FROM table2 - 工作....

2 个答案:

答案 0 :(得分:1)

您需要在查询中添加Field3

SELECT t.field1, t.field2 
FROM (
    SELECT field1, field2, Field3
    FROM table1 
    UNION ALL 
    SELECT field1, field2, field3 FROM table2
) as t
WHERE t.field3=1

使用新查询进行编辑时,您需要以下内容:

SELECT X.field1, X.field2 
FROM
(
    SELECT t1.field1, t1.field2, t1.field3 
    FROM table1 t1
    INNER JOIN table3 t3 ON t1.id=t3.id

    UNION ALL

    SELECT t2.field1, t2.field2, t2.field3 
    FROM table2 t2
    INNER JOIN table4 t4 ON t2.id=t4.id

) as x
WHERE x.field3=1

答案 1 :(得分:0)

我过去发现我需要将UNION的两个方面括起来 - 尝试以下方法:

SELECT X.field1, X.field2 
FROM
(
   (SELECT t1.field1, t1.field2, t1.field3 
    FROM table1 t1
    INNER JOIN table3 t3 ON t1.id=t3.id)

    UNION ALL

   (SELECT t2.field1, t2.field2, t2.field3 
    FROM table2 t2
    INNER JOIN table4 t4 ON t2.id=t4.id)

) as x
WHERE x.field3=1