generate where clause in bash using variables

前端 未结 2 879
谎友^
谎友^ 2021-01-28 02:53

I have some variables in bash like below.

min_date=\'2020-06-06\'
max_date=\'2020-06-08\'
max_seq_min_date=1  --This is till where data is processe         


        
相关标签:
2条回答
  • 2021-01-28 03:06

    It will be more efficient to make minor changes to the query - making it easier to generate (equivalent) SQL dynamically.

    It uses "between" operator to avoid variable length lists for the 'in (...)' conditions.

    Note comment about 1=1, it is kept as per question, but need to be reviewed, as it will always make the condition pass.

    min_date='2020-06-06'
    max_date='2020-06-08'
    max_seq_min_date=1
    max_seq_max_date=3
    
    echo "
    WHERE 1 = 1 or case
        when batch_date = '$min_date' then seq_num between 1 and $max_seq_min_date
        when batch_date = '$max_date' then seq_num between 1 and $max_seq_max_date
        when batch_date between '$min_date' and '$max_date' then seq_num between 1 and 4
        else false
        end
    " 
    

    I do NOT have mysql, but the above works for Postgresql.

    0 讨论(0)
  • 2021-01-28 03:21

    Just use interpolation, i.e.

    sql_clause="... batch_date = '$min_date' and seq_num in ('2','3', '4') ...."
    
    0 讨论(0)
提交回复
热议问题