我有以下疑问:
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]
如何返回每个匹配项的列名或某种标志,以便返回每个经理的职位,例如:
first_name, last_name, position
joe soap manager
john doe ops manager
最佳答案
您可以在选择部分中使用CASE WHEN。
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 ...
关于sql - mysql为每个匹配设置一个标志,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1623562/