我正在尝试创建搜索功能,以便用户可以组合两个值在联系人列表中搜索人。我希望数据库返回两个值都匹配的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入门者的最佳问候
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%'
);