简体   繁体   中英

what's wrong with this mysql join?

SELECT p1.userid, p1.username, p2.lids 
        FROM VB_user p1
        LEFT JOIN AB_judge_perm p2 on p1.userid = p2.userid
        WHERE p1.membergroupids LIKE '".19.",%'
        OR p1.membergroupids LIKE '%,".19."'
        OR p1.membergroupids LIKE '%,".19.",%'
        OR p1.membergroupids = '".19."'

I'm thinking p1.membergroupids doesn't play well with joins? If I remove the p1.membergroupids conditionals, the query works like I wanted to work, exept I want it to only show users that are part of group 19, thus the LIKE s. The p1.membergroupids is a multivalue attribute.

As pointed out by @muistooshort you should move your membergroupids to a many-to-many join table and this problem will disappear. Hideously unnormalised structures like this will cause you untold amounts of pain! Create a new join table - member_group(member_id, group_id)

If you insist on using this hideous structure you can replace your mulitple OR conditions with FIND_IN_SET -

SELECT p1.userid, p1.username, p2.lids 
FROM VB_user p1
LEFT JOIN AB_judge_perm p2
    ON p1.userid = p2.userid
WHERE FIND_IN_SET(19, p1.membergroupids)

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