求一个sql语句同时满足条件

 select temp1.*,
               temp2.self_evaluation
        from declare_key_tasks_details temp1
        LEFT JOIN `declare_month_key_tasks_complete_status` temp2
        ON temp1.id = temp2.task_id
        where
            temp1.responsible_department = ' '
            and temp1.task_type = 
            and temp2.self_evaluation IS NULL OR temp2.self_evaluation!= 4

             and temp2.expected_results != 1

需要满足 self_evaluation 可以为空   并且同时满足 self_evaluation!= 4 expected_results != 1    

 

可以为空,还是必须为空?
如果只是可以为空,那不需要写任何条件

别把 and 条件 和 or条件写在同一级
and 表示必须都满足 ,or表示任意满足,放同一级,前面的and的限制就没起到作用了
如果想满足条件1,且条件2和条件3任意满足一个即可的话,应该像下面这样写

条件1 and (条件2 or 条件3)

另外,你题目里说的 既为空又不等于4, 这是矛盾的,不知道你是不是想表达下面这个意思

 temp1.responsible_department = ' ' and 
temp1.task_type='' and (
temp2.self_evaluation is null or  
(temp2.self_evaluation!= 4  and temp2.expected_results != 1)
)

不管你使用等于或者不等于都会把空的去掉,
这种保留空但不等于某个特定值的有两种写法
方法一

条件1 and (a is null or a!='123') and 条件2

注意中间的 括号是要当成一个整体,去掉括号逻辑就错了

方法二

nvl(字段名,0)!=4

其实就是把空值变成不是空的,这样就可以判断了,但是这样就用不上索引了

select temp1.*,
temp2.self_evaluation,
temp2.expected_results
from declare_key_tasks_details temp1
LEFT JOIN declare_month_key_tasks_complete_status temp2
ON temp1.id = temp2.task_id
where
temp1.responsible_department = ''
and temp1.task_type =
and ((temp2.self_evaluation <> 4 OR temp2.self_evaluation IS NULL )
OR (temp2.expected_results <> 1 OR temp2.expected_results IS NULL))

自己尝试尝试出来了,如果第一个括号里面的or用and来表达两个条件都满足时,他满足任意一个都会查不出来,用or则必须两个字段都不等于才不能查询出来,
还没有弄清楚