需求:通过多个字段进行筛选查询,这些字段可能为空,当为空时不作为筛选条件;
解决办法:where中使用 (字段名=条件 or 条件="")
例:
sql="select id,name from student where (sno='%(sno)s' or '%(sno)s'='') and (identity='%(identity)s' or '%(identity)s'='')" % {
'sno':sno,
'identity':identity,
}
参考博客
https://blog.csdn.net/weixin_45337377/article/details/106307341?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param