开发者

mysql set a flag for each match

开发者 https://www.devze.com 2022-12-09 16:17 出处:网络
I have the following query: SELECT users_extra.first_name, users_extra.last_name FROM (branches, users_extra)

I have the following query:

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 sort of flag for each match so i can return the p开发者_开发百科osition of each manager, eg:

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


You can use CASE WHEN in 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 ...
0

精彩评论

暂无评论...
验证码 换一张
取 消