我有一个查询,我使用 2 UNIONS 来获得我需要的结果
可以使用 CASE 语句来完成,但我无法弄清楚。如果有人能解决这个问题,我将不胜感激
这是我当前的查询,请记住 viewable_by 只能是 1、2 或 3 但是,client_id、team_id 和 created_by 将是一个变量,具体取决于用户,它们会改变
SELECT view_name, view_id, viewable_by FROM `phone_call_views`
WHERE status = 1 AND ( viewable_by = 1 AND client_id = 1)
UNION
SELECT view_name, view_id, viewable_by FROM `phone_call_views`
WHERE status = 1 AND ( viewable_by = 2 AND team_id =5 AND client_id = 1)
UNION
SELECT view_name, view_id, viewable_by FROM `phone_call_views`
WHERE status = 1 AND ( viewable_by = 3 AND created_by= 6)
这是我在 php 中使用变量的实际查询
$db->getDataSet(' SELECT view_name, view_id, viewable_by FROM `phone_call_views`
WHERE status = 1 AND ( viewable_by = 1 AND client_id = '.CLIENT_ID.')
UNION
SELECT view_name, view_id, viewable_by FROM `phone_call_views`
WHERE status = 1 AND ( viewable_by = 2 AND team_id ='.TEAM_ID.' AND client_id = '.CLIENT_ID.')
UNION
SELECT view_name, view_id, viewable_by FROM `phone_call_views`
WHERE status = 1 AND ( viewable_by = 3 AND created_by= '.USER_ID.') '
);