需求:通过多个字段进行筛选查询,这些字段可能为空,当为空时不作为筛选条件;
解决办法: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,
}
参考博客