MySQL找出select语句中使用了哪个WHERE子句

时间:2018-03-23 16:20:14

标签: mysql select where-clause

我的select条款中有多个... or ...条件符合where语句(下方),(很快会有30 + or个条件)

我想要做的是,有一个列告诉我or子句中的where被触发了。

这可能吗?

我试过搜索,但我似乎撞了一堵墙。任何建议或正确方向的观点都将受到赞赏。谢谢

select
    Details_TransactionDate as Transaction_Date,
    Details_Credit as Credit,
    MSD.TicketNo as Ticket_Number,
    Details_Pn as Description,
    MSD.Nun as Numbers
from
    MS_Transactions MST
    left join MBS_Details as MSD on MST.TicketNo = MSD.TicketNo
where 
    Details_Pn like '%SALARY%'
    or Details_Pn like '%Mid MTH%'
    or Details_Pn like '%Mid Month%'
    or Details_Pn like '%Pay%'
    or Details_PNarration like '%SAL%'  
        and day(Details_TransactionDate) between 23 and 25
        and weekday(Details_TransactionDate) Between 0 and 4;

1 个答案:

答案 0 :(得分:1)

为了避免混淆,你应该在OR周围使用proper() 并为每列使用适当的表anem别名 并且您可以使用CASE知道使用哪个OR

Select 

    MST.Details_TransactionDate as Transaction_Date,
    MST.Details_Credit as Credit,
    MSD.TicketNo as Ticket_Number,
    MST.Details_Pn as Description,
    MSD.Nun as Numbers,
    case 
      WHEN MST.Details_Pn like '%SALARY%' THEN 'Details_Pn SALARY'
      WHEN MST.Details_Pn like '%Mid MTH%' THEN 'Details_Pn Mid MTH'
      WHEN MST.Details_Pn like '%Mid Month%' THEN 'Details_Pn Mid Month'          
      WHEN MST.Details_Pn like '%Pay%' THEN 'Details_Pn Pay'
      WHEN MST.Details_PNarration like %SAL%' THEN 'Details_PNarrationSAL'
    end or_used 

from MS_Transactions MST
Left join MBS_Details as MSD on MST.TicketNo = MSD.TicketNo

where  ( MST.Details_Pn like '%SALARY%'
OR MST.Details_Pn like '%Mid MTH%'
OR MST.Details_Pn like '%Mid Month%'
OR MST.Details_Pn %Pay%
OR MST.Details_PNarration like '%SAL%'  ) 
AND DAY(MST.Details_TransactionDate) between '23' and '25' and 
    WEEKDAY(MST.Details_TransactionDate) Between 0 and 4;