Mysql通过使用相关名来与子查询连接连接

时间:2015-02-19 18:18:34

标签: mysql join

我试图使用MySQL连接子查询,子查询也有连接所以我面临问题,请善待,因为我不是没有尝试,我尽力解决但是得到语法错误这是因为相关名称为t1和t2

SELECT min(user_org.oid) as id, user_org_profiles.name from user_org
LEFT JOIN user_org_profiles ON (user_org.oid = user_org_profiles.oid) t1
INNER JOIN
    (SELECT user_org_profiles.name, max(user_org.gid) as gid
     FROM user_org LEFT JOIN user_org_profiles USING(oid)
     WHERE user_org.gid IN (1,2]) AND active =1 
     group by name) t2 ON t1.name = t2.name AND t1.gid = t2.gid group by t1.name

纠正此错误

1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在']附近使用正确的语法,并且按名称使用活动= 1组)ON t1.name = t2.name AND t1.gid = t2。在第6行

1 个答案:

答案 0 :(得分:1)

修复了user_org_profiles的别名为t1加上IN列表中的小错误的问题。这些错误与消息中报道的文字并不十分接近。

SELECT min(user_org.oid) as id, t1.name from user_org
LEFT JOIN user_org_profiles t1 ON (user_org.oid = t1.oid)
INNER JOIN
    (SELECT user_org_profiles.name, max(user_org.gid) as gid
     FROM user_org LEFT JOIN user_org_profiles USING(oid)
     WHERE user_org.gid IN (1,2) AND active = 1 
     group by name) t2 ON t1.name = t2.name AND t1.gid = t2.gid group by t1.name