0

我正在尝试在 MySQL 中进行 FULL OUTER JOIN,我们都知道这是不可能的。所以,就像我正在使用的任何普通修补匠一样:

(T1 左外连接 T2) UNION (T1 右外连接 T2)

现在这是问题所在!每个单独的连接(左和右)都像一个魅力,但它们一起给出一个错误(1064:语法错误),并且提示我检查我的 MySQL 服务器手册以获取正确的语法。

这是代码:

(SELECT 
    *
FROM
    result LEFT OUTER JOIN 
    ((SELECT * FROM SQA AS sqa JOIN SA AS sa USING (sa_oid) WHERE sqa.sq_oid = 1)
    AS tmp1) ON tmp1.sa_oid = result.re_as

WHERE
    result.re_p = 1 AND 
    result.re_s = 1 AND
    result.re_q = 1)

UNION

(SELECT 
    *
FROM
    result RIGHT OUTER JOIN 
    ((SELECT * FROM SQA AS sqa JOIN SA AS sa USING (sa_oid) WHERE sqa.sq_oid = 1)
    AS tmp2) ON tmp2.sa_oid = result.re_as

WHERE
    result.re_p = 1 AND 
    result.re_s = 1 AND
    result.re_q = 1)

同样,两个左连接和右连接单独工作,但如果与 UNION 运算符放在一起,似乎会产生语法错误。有任何想法吗?有名称冲突吗?或者在这种情况下不可能进行 FULL OUTER JOIN ?

非常感谢任何反馈!另外,请原谅这个问题的琐碎(如果它是琐碎的)。

编辑:

"Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON tmp2.sa_oid = result.re_as

    WHERE
        result.re_p = 1 AND 
        result.re' at line 20"
4

1 回答 1

1
Try this:

(SELECT 
    *
FROM
    result LEFT OUTER JOIN 
    (SELECT * FROM surveyquestion_answers AS sqa JOIN surveyanswer AS sa USING (sa_oid) WHERE sqa.sq_oid = 1)
    AS tmp1 ON tmp1.sa_oid = result.re_answer

WHERE
    result.re_page = 1 AND 
    result.re_survey = 1 AND
    result.re_question = 1)

UNION

(SELECT 
    *
FROM
    result RIGHT OUTER JOIN 
    (SELECT * FROM surveyquestion_answers AS sqa JOIN surveyanswer AS sa USING (sa_oid) WHERE sqa.sq_oid = 1)
    AS tmp2 ON tmp2.sa_oid = result.re_answer

WHERE
    result.re_page = 1 AND 
    result.re_survey = 1 AND
    result.re_question = 1)
于 2013-01-25T17:15:54.967 回答