【问题标题】:How to filter rows data from sqlite table with a variable from multiple column?如何使用来自多列的变量过滤来自 sqlite 表的行数据?
【发布时间】:2020-05-27 20:54:56
【问题描述】:

这是我用来过滤具有单列变量的行及其工作的代码:

cur.execute("SELECT * FROM Table1 WHERE item1 LIKE ?", ('%'+item_name+'%', ))
n = cur.fetchall()

但我想用一个变量从多列中过滤行。 例如:表格有三列,分别为 item1、item2 和 item3。我想将行过滤为 select * from table1 where item1, item2 and item3。 如果可以的话,请告诉我怎么做?

【问题讨论】:

    标签: python-3.x sqlite


    【解决方案1】:

    您可以将表加入到此查询中:

    SELECT '%' || ? || '%' AS item_name
    

    在你想要的条件下,像这样:

    SELECT t1.* 
    FROM Table1 t1 INNER JOIN (SELECT '%' || ? || '%' AS item_name) t2
    ON t1.item1 LIKE t2.item_name OR t1.item2 LIKE t2.item_name OR t1.item3 LIKE t2.item_name 
    

    您的代码将是:

    cur.execute("SELECT t1.* FROM .....", (item_name, ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-02
      • 2021-01-12
      • 2015-09-29
      • 1970-01-01
      • 2019-07-25
      • 2017-11-11
      • 2022-01-19
      • 2020-04-16
      相关资源
      最近更新 更多