Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

sql - Mysql how to write a query contains where followed by condition

I am writing one query, that needs to satisfy the following conditions.

select 
    field1, field2, field3
from 
    `tabFirstTable`as ft join 
    `tabSecondTable` as st
where 
    --Upto above its fine but how to turn below logic into query-- 
    IF ft.approval_status in == 'Approved By Manager' and ft.status in ("Created", "Error")
        and //some more conditions
    ELSE ft.approval_status in == 'Approved By HOD' and ft.status in ("Initiated")
        and //some more conditions

Because of the above issue currently writing two separate queries, which is not a good idea. Please help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unless I am missing something or over simplifying

WHERE
    (ft.status IN ('Created', 'Error') AND 
    ft.approval_status = 'Approved By Manager' AND 
        //other stuff
    )
OR 
    (ft.status = 'Initiated' AND 
     ft.approval_status = 'Approved By HOD' AND 
        //some more conditions
    )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...