两个外连接上的别名

时间:2014-05-27 12:20:50

标签: mysql join left-join alias

我正在尝试运行查询,在同一个表profile_data上产生2个外连接。所以最终我想要一个表格结果用户|朋友|学位| user_value | friend_value,其中friend_value依赖于relation_degree。第一个外连接工作,但当我添加第二个外连接时,它给了我一个错误。我认为这与别名有关。有人能把我推向正确的方向吗?非常感谢!

         SELECT degrees.User, 
                degrees.friend, 
                degrees.relation_degree, 
                a.wp_bp_xprofile_data.value as user_value, 
                b.wp_bp_xprofile_data.value as user_value
           FROM degrees
LEFT OUTER JOIN a.wp_bp_xprofile_data
             ON degrees.User=wp_bp_xprofile_data.user_id
          WHERE wp_bp_xprofile_data.field_id =  CASE
                                                  WHEN degrees.relation_degree = 1 Then  2
                                                  WHEN degrees.relation_degree = 2 Then  6
                                                  WHEN degrees.relation_degree = 3 Then  7
                                                END
LEFT OUTER JOIN b.wp_bp_xprofile_data
            ON degrees.friend=wp_bp_xprofile_data.user_id
         WHERE wp_bp_xprofile_data.field_id =  CASE
                                                 WHEN degrees.relation_degree = 1 Then  2
                                                 WHEN degrees.relation_degree = 2 Then  6
                                                 WHEN degrees.relation_degree = 3 Then  7
                                               END

3 个答案:

答案 0 :(得分:1)

您似乎有两个表连接到同一个表而没有别名表名,还有2个WHERE子句。

尝试这样的事情: -

SELECT degrees.User, 
        degrees.friend, 
        degrees.relation_degree, 
        wp_bp_xprofile_data1.value as user_value_1, 
        wp_bp_xprofile_data2.value as user_value_2
FROM degrees
LEFT OUTER JOIN wp_bp_xprofile_data wp_bp_xprofile_data1
ON degrees.User=wp_bp_xprofile_data1.user_id
AND wp_bp_xprofile_data1.field_id =  CASE
                                        WHEN degrees.relation_degree = 1 Then  2
                                        WHEN degrees.relation_degree = 2 Then  6
                                        WHEN degrees.relation_degree = 3 Then  7
                                    END
LEFT OUTER JOIN wp_bp_xprofile_data wp_bp_xprofile_data2
ON degrees.friend=wp_bp_xprofile_data2.user_id
AND wp_bp_xprofile_data2.field_id = CASE
                                        WHEN degrees.relation_degree = 1 Then  2
                                        WHEN degrees.relation_degree = 2 Then  6
                                        WHEN degrees.relation_degree = 3 Then  7
                                    END

答案 1 :(得分:1)

中间有位置。 您需要在查询的最后添加 where 。  如果要在结束时过滤联合数据,但在联接期间可以过滤使用 " on .... AND wp_bp_xprofile_data.field_id = CASE ..."

但最重要的是你在桌子上没有别名。

你需要做

from ...
left outer join a.wp_bp_xprofile_data AS firstAlias
on firstAlias.someColumn = ...
...
left outer join a.wp_bp_xprofile_data AS secondAlias
on secondAlias.someColumn = ...

答案 2 :(得分:0)

请检查......

SELECT degrees.User, degrees.friend, degrees.relation_degree, a.wp_bp_xprofile_data.value as user_value1, b.wp_bp_xprofile_data.value as user_value2
From degrees
left outer join a.wp_bp_xprofile_data
on degrees.User=a.wp_bp_xprofile_data.user_id
where a.wp_bp_xprofile_data.field_id =  CASE

WHEN degrees.relation_degree = 1 Then  2
WHEN degrees.relation_degree = 2 Then  6
WHEN degrees.relation_degree = 3 Then  7

END

left outer join b.wp_bp_xprofile_data
on degrees.friend=b.wp_bp_xprofile_data.user_id
where b.wp_bp_xprofile_data.field_id =  CASE

WHEN degrees.relation_degree = 1 Then  2
WHEN degrees.relation_degree = 2 Then  6
WHEN degrees.relation_degree = 3 Then  7

END