如何编写一个从两个搜索值返回匹配项的sql查询

问题描述 投票:1回答:1

我正在尝试创建搜索功能,以便用户可以组合两个值在联系人列表中搜索人。我希望数据库返回两个值都匹配的posts

现在我已经尝试执行此操作:

input = searchValueA, searchValueB

SELECT * FROM ContactsList WHERE lastName LIKE '%searchValueA%' OR firstName LIKE '%searchValueA%' OR company LIKE '%searchValueA%' OR phoneNumber LIKE '%searchValueA%' AND lastName LIKE '%searchValueB%' OR firstName LIKE '%searchValueB%' OR company LIKE '%searchValueB%' OR phoneNumber LIKE '%searchValueB%'; 

但是这会返回与两个搜索值均匹配的帖子。我只想获取两个搜索值都匹配的帖子。我可以重新定义查询以使其执行我想要的吗?

SQL入门者的最佳问候

sql sqlite jdbc
1个回答
1
投票
WHERE (lastName LIKE '%searchValueA%' OR firstName LIKE '%searchValueA%' OR company LIKE '%searchValueA%' OR phoneNumber LIKE '%searchValueA%' ) AND (lastName LIKE '%searchValueB%' OR firstName LIKE '%searchValueB%' OR company LIKE '%searchValueB%' OR phoneNumber LIKE '%searchValueB%' );
© www.soinside.com 2019 - 2024. All rights reserved.