简体   繁体   中英

Mysql join with subquery join with use of correlation names

I trying to use MySQL join with sub-query , sub-query also has join so i am facing problem,Please be kind, because i am not asking without trying, i tried my best to solve but getting syntax error this is because of correlation names t1 and 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

getiing this error

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 ']) AND active =1 group by name) ON t1.name = t2.name AND t1.gid = t2.' at line 6

Fixed problems with aliasing of user_org_profiles as t1 plus a small typo in the IN list. Those mistakes were not very close to the text reported in the message though.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM