Mysql sets a flag for each match

I have the following request:

SELECT users_extra.first_name, users_extra.last_name
FROM (branches, users_extra)
WHERE ((branches.manager_id = users_extra.userid)
OR (branches.sales_manager_id = users_extra.userid)
OR (branches.admin_manager_id = users_extra.userid)
OR (branches.ops_manager_id = users_extra.userid)
OR (branches.export_manager_id = users_extra.userid)
OR (branches.import_manager_id = users_extra.userid))
AND branches.branch_id = [$VAR]

      

How do I return the column name or some kind of flag for each match so that I can return the position of each manager, like this:

first_name, last_name, position
joe         soap       manager
john        doe        ops manager

      

+2


source to share


1 answer


You can use CASE WHEN on the SELECT part.



SELECT users_extra.first_name, users_extra.last_name,
    CASE users_extra.userid
    WHEN branches.manager_id THEN 'manager'
    WHEN branches.sales_manager_id  THEN 'sales manager'
    ...
    END AS position
FROM ...

      

+1


source







All Articles