sql - 如何从sql server中的join替换OR语句
问题描述
我有以下查询,它在连接上使用 or 语句,所以基本上如果连接上的一个条件不满足,它必须检查下一个条件。问题在于,使用 OR 语句需要很长时间才能运行,但是当我删除其中一个 OR 条件时,它会立即运行。有没有更好的方法在不使用 OR 语句的情况下对这两个条件执行此操作,这样可以加快查询速度
select t5.TransactionNumber
,t4.ID
,t3.[Entry] AS Amount
,t2.Address AS AddressDetail
,t1.PhoneNumber AS ContactNumber
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t2.FicaID = t1.FicaId
inner join Table3 t3 (NOLOCK) ON (t3.ID = t2.ID AND t3.Code = t2.Code) or (t3.TypeID = t2.TypeID) //on this join i have an or statement if one condition isnt met it must check the next condition
LEFT JOIN Table4 t4 (NOLOCK) ON t4.Result = t3.Result
LEFT JOIN Table5 t5 (NOLOCK) ON t5.AccNum = t3.AccNum
where t1.date>'2018-09-01' and t1.date<'2018-09-30'
解决方案
根据逻辑中的分配规则,
P OR (Q AND R) 可以写成 (P OR Q) AND (P OR R).. 也许这有帮助?
推荐阅读
- amazon-cognito - 使用 .Net Core 3 的 AWS Cognito 预身份验证 lambda 触发器 - 返回“InvalidLambdaResponse”异常
- websocket - 使用 SocketIO 如何检查在房间内广播时未发送消息?
- processing - 在 p5js 中创建星星的数学原理是什么
- javascript - 如何更改每个键入的 javascript 字母的字体粗细
- sql - 如何在 Oracle SQL (Oracle Forms 12c) 中将多个列值组合成单个值作为逗号分隔的字符串
- python - 安装 scikit-image 包时 anaconda 中的 InvalidArchiveError
- javascript - 将当前时间与 ngb-timepicker 中输入的时间进行比较
- python - 尝试访问 Binance API 时出错:“Binance”对象没有属性“binance_keys”
- android - 如何从字符串文件(getResources)android中获取TabLayout的名称
- php - 语法错误,意外 ')',期待 '[' (查看:C:\project\laravel\project1\app\resources\views\products.blade.php)