我需要按几个条件过滤我的任务表。
select * from tasks where initialPrice > 20 where status = 3
但是如果这个任务属于 userId = 1(例如) - 我需要用其他附加状态(状态 = 1 和状态 = 5)来选择它。所以我的变体是:
select * from tasks where initialPrice > 20 and status = 3 or (status = 1 or status = 5 and userId = 5 and initialPrice > 20)
是否可以避免条件重复以及构建此查询的最佳方法是什么?
PS进一步,此查询将与多个连接一起使用,并且很难在此查询的两个部分中复制连接和条件。
试试这个
select * from tasks
where initialPrice > 20
and case when status = 3 Then 1 when status in (1,5) and userId = 5 Then 1 else 0 end = 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句