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 ...
精彩评论