我正在创建一种方法,其中用户可以按名字,姓氏或全名搜索客户。搜索栏仅显示按名字或姓氏显示的结果,但是当我尝试按全名搜索客户时,记录将不会显示。
这是我的代码:
public int getMatchingCustomerRecords(String keyword) {
int rows = 0;
try {
if (db.startTransaction()) {
String sql = "Select COUNT(firstName OR lastName) AS TOTAL FROM Customer";
String sqlSearch = "";
if (keyword != null) {
sqlSearch = sql + " WHERE firstName LIKE '%" + keyword + "%' AND lastName LIKE '%" + keyword + "%'";
ps = db.getQueryStatement(sqlSearch);
} else {
ps = db.getQueryStatement(sql);
}
rs = ps.executeQuery();
if (rs.next()) {
rows = rs.getInt("TOTAL");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
最佳答案
您的查询应为:
Select * FROM `Customer`
WHERE `firstName` LIKE '%" + keyword + "%' OR `lastName` LIKE '%" + keyword + "%'"
关于java - 如何按全名搜索客户?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55969991/