在sqlite SELECT FROM中搜索多个列

t5zmwmid  于 2023-03-03  发布在  SQLite
关注(0)|答案(4)|浏览(185)

我使用这段代码来搜索A列。如果我想同时搜索A列和B列,我应该做什么修改?

String sql = "SELECT * FROM " + TABLE_NAME +
                " WHERE " + ColumnA + " LIKE ? ORDER BY " + ColumnA + " LIMIT 100";

我试过了,但没用:

String sql = "SELECT * FROM " + TABLE_NAME +
                " WHERE " + ColumnA + " LIKE ? ORDER BY " + ColumnA + " LIMIT 100 OR SELECT * FROM " + TABLE_NAME +
                " WHERE " + ColumnB + " LIKE ? ORDER BY " + ColumnB + " LIMIT 100";

我做错什么了吗?

mxg2im7a

mxg2im7a1#

您可以使用OR连接两个表达式。但是,两个表达式都必须是完整的表达式:

SELECT ... WHERE ColA LIKE ? OR ColB LIKE ? ...
baubqpgj

baubqpgj3#

进行两个不同的查询。并通过union关键字合并结果。

select * from TABLE_NAME where columnA like ? order by columnA limit 100
union
select * from TABLE_NAME where columnB like ? order by columnA limit 100
fzsnzjdm

fzsnzjdm4#

致OP:
在您的答案中,过滤必须是每个字段的OR分别针对LIKE ?
因此,该行应为String sql = "SELECT * FROM " + TABLE_NAME + " WHERE ( " + ColumnA + " LIKE ?) OR (" + ColumnB + " LIKE ?) ORDER BY " + ColumnA + " LIMIT 100";

相关问题