简体   繁体   中英

FIND_IN_SET in mysql

I have a query that grabs info from a bunch of tables, including setting two columns to either 1 or 0 depending on the value of a field in another table; you can ignore the vast majority of it - I'm only concerned with the two IF lines:

SELECT DISTINCT CONCAT(np.first_name,' ',np.last_name) AS name, np.last_name, np.id, np.title, nc.web_name AS company, xsp.image_file AS image,

IF(prx.people_role_id IN(7,8),1,0) AS rfac,
IF(prx.people_role_id = 8,1,0) AS dfac

FROM expo_speaker xsp

LEFT JOIN expo_session_speaker xssx ON xssx.speaker_id = xsp.speaker_id
LEFT JOIN expo_session xss ON xss.session_id = xssx.session_id
LEFT JOIN new_people np ON np.id = xsp.people_id
LEFT JOIN people_role_xref prx ON prx.people_id = np.id
LEFT JOIN new_company nc ON np.company_id = nc.id

WHERE np.active = 1
AND xss.session_id = $sym->id
GROUP BY np.title
ORDER BY np.last_name

This worked just fine when each person could only have one role (represented by prx.people_role_id). However, requirements changed recently, and each user can now have anywhere from zero to twelve different roles (and that number can change at any time.)

I've looked into find_in_set, but can't figure out how to combine it with the IN clause. I've tried group_concat(prx.people_role_id) , which gives me a comma-delimited list of all the roles assigned to a user. If there were only one role that we were looking for, it would be easy to use find_in_set to see if that role existed in the list of assigned roles. But we have two possible roles for one of the IF statements. How can I combine these functions (or any other) to determine if the group_concat column contains either 7 or 8?

Got it;

IF((find_in_set(7, group_concat(prx.people_role_id))) OR (find_in_set(8, group_concat(prx.people_role_id))),1,0) AS rfac,
IF(find_in_set(8, group_concat(prx.people_role_id)),1,0) as dfac

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