SQLite IN运算符在for循环中具有多个条件

yqyhoc1h  于 2023-03-19  发布在  SQLite
关注(0)|答案(1)|浏览(174)

我有以下内容:

x = ['dat1','dat2']
y = ['M','F']

for x,y in zip(x,y):
    conn.execute('''
                create table %s as
                select sex as sex
                from raw
                where sex in (?)
                '''%(x),(y )
                )

conn.commit()

如何设置for循环/列表来捕获M和F字符串沿着单独的M和F,而不使用连接?我尝试了各种列表,例如y = ['M','F','"M","F"'],但它们都不起作用(我也在x中包含了'dat 3')。是否需要format()

zengzsys

zengzsys1#

可以使用LIKE运算符完成此操作:

x = ['dat1','dat2','dat3']
y = ['M%','F%','%']

for x,y in zip(x,y):
    conn.execute('''
                create table %s as
                select sex as sex
                from raw
                where sex like ?
                '''%(x),(y, )
                )

conn.commit()

相关问题