SQLite WHERE-Clause for every column?

不打扰是莪最后的温柔 提交于 2021-01-28 22:21:21

问题


Does SQLite offer a way to search every column of a table for a searchkey?

SELECT * FROM table WHERE id LIKE ...

Selects all rows where ... was found in the column id. But instead to only search in the column id, I want to search in every column if the searchstring was found. I believe this does not work:

SELECT * FROM table WHERE * LIKE ...

Is that possible? Or what would be the next easy way?

I use Python 3 to query the SQLite database. Should I go the route to search through the dictionary after the query was executed and data returned?


回答1:


A simple trick you can do is:

SELECT *
FROM table
WHERE ((col1+col2+col3+col4) LIKE '%something%')

This will select the record if any of these 4 columns contain the word "something".




回答2:


No; you would have to list or concatenate every column in the query, or reorganize your database so that you have fewer columns.

SQLite has full-text search tables where you can search all columns at once, but such tables do not work efficiently with any other queries.




回答3:


I'm not quite sure, if I understood your question. If you want the whole row returned, when id=searchkey, then:

select * from table where id=searchkey;

If you want to have specific columns from the row with the correct searchkey:

select col1, col2, col3 from table where id=searchkey;

If you want to search multiple columns for the "id": First narrow down which columns this could be found in - you don't want to search the whole table! Then:

select * from table where col1=searchkey or col2=searchkey or col3=searchkey;


来源:https://stackoverflow.com/questions/30732480/sqlite-where-clause-for-every-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!