Mysql 简化从bash脚本生成的where子句

Mysql 简化从bash脚本生成的where子句,mysql,sql,bash,Mysql,Sql,Bash,我在bash source_FROM_batch_date='2020-06-06' source_to_batch_date='2020-06-07' min_batch_date_seq_num=2 max_batch_date_seq_num=3 “我的数据加载”以批处理方式运行,批处理从1到4,并且4是最大批处理 我想根据上面的变量动态生成一个where子句 batch_date和batch_seq_num是我将过滤数据的列 条件 1) read all the data where

我在
bash

source_FROM_batch_date='2020-06-06'
source_to_batch_date='2020-06-07'
min_batch_date_seq_num=2
max_batch_date_seq_num=3
“我的数据加载”以批处理方式运行,批处理从
1
4
,并且
4
是最大批处理

我想根据上面的变量动态生成一个
where
子句

batch_date和batch_seq_num是我将过滤数据的列

条件

1) read all the data where batch_date = '$source_FROM_batch_date' and batch_seq_num >= 'min_batch_date_seq_num'
2) read all the data where batch_date = '$source_to_batch_date' and batch_seq_num <= 'max_batch_date_seq_num'
3) read all the data that occurs between $source_FROM_batch_date and $source_to_batch_date
输出

(
(batch_date='2020-06-06' AND batch_seq_num='2') OR 
(batch_date='2020-06-06' AND batch_seq_num='3') OR 
(batch_date='2020-06-06' AND batch_seq_num='4') OR 
(batch_date='2020-06-07' AND batch_seq_num='1') OR 
(batch_date='2020-06-07' AND batch_seq_num='2') OR 
(batch_date='2020-06-07' AND batch_seq_num='3') 
)
(
(batch_date='2020-06-06' AND batch_seq_num in ('2', '3', '4') OR 
(batch_date='2020-06-07' AND batch_seq_num in ('1', '2', '3') 
)
所需输出

(
(batch_date='2020-06-06' AND batch_seq_num='2') OR 
(batch_date='2020-06-06' AND batch_seq_num='3') OR 
(batch_date='2020-06-06' AND batch_seq_num='4') OR 
(batch_date='2020-06-07' AND batch_seq_num='1') OR 
(batch_date='2020-06-07' AND batch_seq_num='2') OR 
(batch_date='2020-06-07' AND batch_seq_num='3') 
)
(
(batch_date='2020-06-06' AND batch_seq_num in ('2', '3', '4') OR 
(batch_date='2020-06-07' AND batch_seq_num in ('1', '2', '3') 
)
如何实现多批次的目标

wclause="(batch_date=... and batch_seq_num in"
然后在每个seq的循环内:

wclause="${wclause}(${start_seq}"    # for first seq
wclause="${wclause},${start_seq}"    # rest of seq's
退出循环后:

wclause="${wclause}))"
echo "${wclause}"
对于多批次:

wclause="(batch_date=... and batch_seq_num in"
然后在每个seq的循环内:

wclause="${wclause}(${start_seq}"    # for first seq
wclause="${wclause},${start_seq}"    # rest of seq's
退出循环后:

wclause="${wclause}))"
echo "${wclause}"

你刚才不是问了很多这个问题吗?有一条评论链接到,强调“最小值”?这个问题看起来很熟悉…@BenjaminW.:@BenjaminW。是的,我之前问过同样的问题。在生成输出时,我已经用我想要的条件更新了问题。我已经包括了我的代码和我尝试过的代码。我有我正在接收的输出和我预期的输出。@Cyrus我已经包括了所需的条件,并给出了预期的结果和我正在接收的结果。我的代码如果按原样运行将生成我的输出。如果这仍然不是一个理想的问题,请告诉我。如果不仔细看,这真的是再现您所看到的问题的最少量的代码吗?没有任何东西可以删除?你刚才不是问了很多这个问题吗?有一条评论链接到,强调“最小”?这个问题看起来很熟悉…@BenjaminW.:@BenjaminW。是的,我之前问过同样的问题。在生成输出时,我已经用我想要的条件更新了问题。我已经包括了我的代码和我尝试过的代码。我有我正在接收的输出和我预期的输出。@Cyrus我已经包括了所需的条件,并给出了预期的结果和我正在接收的结果。我的代码如果按原样运行将生成我的输出。如果这仍然不是一个理想的问题,请告诉我。如果不仔细看,这真的是再现您所看到的问题的最少量的代码吗?没有任何东西可以删除?我尝试了您的解决方案,但得到了
((batch_date=…和batch_seq_num in,(,(,()))
结果。如果您使用的是
${seq}
,请指定在我的代码中放置代码部分的正确位置。。。这将是一个问题;我给了您一个一般性的指导原则,假设您将用实际的变量名替换
${seq}
;我已经用你的变量名更新了答案;至于你的第二个问题。。。您是如何生成刚才在评论中发布的字符串的?我尝试了您的解决方案,但得到了
((batch_date=…和batch_seq_num in,(,(,())))
结果。如果您使用的是
${seq}
,请指定在我的代码中放置代码部分的正确位置。。。这将是一个问题;我给了您一个一般性的指导原则,假设您将用实际的变量名替换
${seq}
;我已经用你的变量名更新了答案;至于你的第二个问题。。。您是如何生成刚才在评论中发布的字符串的?