Python怎样定位并删除Sql语句中不确定的查询条件

1.问题场景描述:

        在sql语句中经常会有查询条件是:查找多个订单签订日期范围的数据,但具体的日期范围是不确定,我们如何来查找定位

例如:查询条件语句的部分如下图:

目标是:

        1)定位字符串:t_contract_order.sign_date

        2)最终得到结果:

解决问题思路:

        1)定位要找的字符串起始位置和截止位置

                起始位置的定位:    

#--------定位字符串出现的位置
pattern = "t_contract_order.sign_date"
# 使用 re.finditer 查找所有出现的位置
positions = [match.start() for match in re.finditer(pattern, teststr)]

                结束位置的查找技巧是找到第二次出现单引号的位置,使用函数

def find_char_second_occurrence(s, start, char):
    first_index = s.find(char, start)
    if first_index == -1:
        return -1
    second_index = s.find(char, first_index + 1)
    return second_index if second_index != -1 else first_index

        2)去除相关内容,保持sql其他内容的正确性和完整性

for tmpstr in tmpstrs:
    print(tmpstr)
    tstr=' and '+tmpstr
    print(tstr)
    teststr=teststr.replace(tstr,'')
    tstr = ' or ' + tmpstr
    teststr = teststr.replace(tstr, '')
    tstr = tmpstr +' and '
    teststr = teststr.replace(tstr, '')
    tstr = tmpstr + ' or '
    teststr = teststr.replace(tstr, '')
    tstr = tmpstr
    teststr = teststr.replace(tstr, '')
    print(f'sql={teststr}')

最后将完整代码提供给大家:

import re
def find_char_second_occurrence(s, start, char):
    first_index = s.find(char, start)
    if first_index == -1:
        return -1
    second_index = s.find(char, first_index + 1)
    return second_index if second_index != -1 else first_index


# sql查询语句测试使用
teststr = "where t_contract_order.sign_date<='2019-01-01 00:00:00'" \
          " and a=b or t_contract_order.sign_date>='2024-01-01 00:00:00'" \
          " and t_contract_order.sign_date<='2024-12-31 23:59:59'" \
          " or c=d or t_contract_order.sign_date>='2025-02-28 23:59:59'"

#--------定位字符串出现的位置
pattern = "t_contract_order.sign_date"
# 使用 re.finditer 查找所有出现的位置
positions = [match.start() for match in re.finditer(pattern, teststr)]
print(f'字符串{pattern}出现的位置{positions}')
character = "'"
tmpstrs=[]
for position in positions:
    second_occurrence = find_char_second_occurrence(teststr, position, character)
    tmpstrs.append(teststr[position:second_occurrence+1])
    print(position,second_occurrence,teststr[position:second_occurrence+1])
for tmpstr in tmpstrs:
    print(tmpstr)
    tstr=' and '+tmpstr
    print(tstr)
    teststr=teststr.replace(tstr,'')
    tstr = ' or ' + tmpstr
    teststr = teststr.replace(tstr, '')
    tstr = tmpstr +' and '
    teststr = teststr.replace(tstr, '')
    tstr = tmpstr + ' or '
    teststr = teststr.replace(tstr, '')
    tstr = tmpstr
    teststr = teststr.replace(tstr, '')
    print(f'sql={teststr}')

如果对你有所帮助,请帮忙点赞+关注+分享

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿桂天山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值