简体   繁体   中英

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)

What I want to do with this, is to have a column which tells me which or in the where clause was triggered.

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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