在sqlite SELECT FROM中搜索多列

问题描述 投票:0回答:4

我使用这段代码来搜索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";

我有什么错误吗?

android sqlite
4个回答
1
投票

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

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

0
投票

您是否在寻找 UNION 而不是 OR? http://www.tutorialspoint.com/sqlite/sqlite_unions_clause.htm


0
投票

进行两个不同的查询。并通过

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

0
投票

致OP:

在您的答案中,过滤必须是每个字段的

OR
分别针对
LIKE ?

所以这条线应该是

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

© www.soinside.com 2019 - 2024. All rights reserved.