我使用此代码搜索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";
我犯了什么错误?
答案 0 :(得分:1)
您可以使用OR连接两个表达式。 但是,两者都必须是完整的表达式:
SELECT ... WHERE ColA LIKE ? OR ColB LIKE ? ...
答案 1 :(得分:0)
你在寻找UNION而不是OR吗? http://www.tutorialspoint.com/sqlite/sqlite_unions_clause.htm
答案 2 :(得分: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
答案 3 :(得分:-1)
这解决了我的问题:
String sql = "SELECT * FROM " + TABLE_NAME +
" WHERE " + ColumnA + " OR " + ColumnB + " LIKE ? ORDER BY " + ColumnA + " LIMIT 100";
我应该使用一个select语句。