简体   繁体   English

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

[英]MySQL find out which WHERE clause was used in select statement

I've got a select statement (below) with multiple ... or ... connected conditions in my where clause, (soon to be 30+ or conditions) 我的where子句中有一个带有多个... or ...连接条件的select语句(如下)(很快便是30+ or条件)

What I want to do with this, is to have a column which tells me which or in the where clause was triggered. 我想要做的是有一列告诉我触发了where or where子句的那一列。

Is this possible? 这可能吗?

I've tried searching, but I seem to have hit a wall. 我尝试搜索,但似乎碰壁了。 Any advice or a point in the right direction would be appreciated. 任何建议或正确方向的观点将不胜感激。 Thank you 谢谢

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;

for avoid confusion you should use proper () around the OR and use proper table anem Alias for each column and you could use a CASE for know which OR is used 为避免混淆,您应该在OR周围使用适当的(),并对每列使用适当的表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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM