SQLite condition

后端 未结 2 642
眼角桃花
眼角桃花 2021-01-25 17:50

I just want to convert this function query from navicat to sqlite query,

 Select * from tbl_sample where ID = 1 And IF (
        RECEIPT_MODE = \'MANUAL\',
              


        
相关标签:
2条回答
  • 2021-01-25 18:03

    Try this query:

    SELECT *
    FROM tbl_sample
    WHERE ID = 1 AND
    (
        (RECEIPT_MODE = 'MANUAL' AND
         DATE(a.MANUAL_COLLECTION_DATE) = DATE('2016-08-10 04:18:00')) OR
        (RECEIPT_MODE <> 'MANUAL' AND
         DATE(TPAY_ISSUED_DATE) = DATE('2016-08-10 04:18:00'))
    )
    

    Use the DNF to CNF rewrite rule to reduce the level of parens/nesting:

    SELECT *
    FROM tbl_sample
    WHERE ID = 1 
    AND ( RECEIPT_MODE <> 'MANUAL' OR
          DATE(a.MANUAL_COLLECTION_DATE) = DATE('2016-08-10 04:18:00') )
    AND ( RECEIPT_MODE = 'MANUAL' OR 
          DATE(TPAY_ISSUED_DATE) = DATE('2016-08-10 04:18:00') )
    
    0 讨论(0)
  • 2021-01-25 18:03

    The standard SQL mechanism for that IF would be a CASE expression:

    Select * from tbl_sample where ID = 1 And CASE
           WHEN RECEIPT_MODE = 'MANUAL'
           THEN DATE(a.MANUAL_COLLECTION_DATE) = DATE('2016-08-10 04:18:00')
           ELSE DATE(TPAY_ISSUED_DATE) = DATE('2016-08-10 04:18:00')
       END
    
    0 讨论(0)
提交回复
热议问题