首页 > 解决方案 > 如何从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'

标签: sqltsql

解决方案


根据逻辑中的分配规则,

P OR (Q AND R) 可以写成 (P OR Q) AND (P OR R).. 也许这有帮助?


推荐阅读