user3030327
user3030327

Reputation: 451

How to filter rows data from sqlite table with a variable from multiple column?

Here is the code i am using to filter rows with a variable from single column and Its working:

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

But i want to filter rows with one variable from multiple columns. For example: Table has three columns as item1, item2 and item3. I want to filter rows as select * from table1 where item1, item2 and item3. If it is possible, please let me know How to do?

Upvotes: 1

Views: 450

Answers (1)

forpas
forpas

Reputation: 164099

You can join the table to this query:

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

on the conditions that you want, like this:

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 

and your code will be:

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

Upvotes: 1

Related Questions