我想我在这里阅读了所有文章,但仍然无法让我的代码 100% 工作。我是一个 PHP 表单,用户可以根据 4 个位置中的任何一个进行搜索,也可以完全跳过它,在这种情况下应该跳过“where location”子句并移至下一个参数组。我的 mysql 存储过程如下所示:
CREATE PROCEDURE search(
IN inRoomsPerPage INT,
IN inStartItem INT,
IN inAddDate INT,
IN inLocation1 ENUM('T','F'),
IN inLocation2 ENUM('T','F'),
IN inLocation3 ENUM('T','F'),
IN inLocation4 ENUM('T','F')
)
BEGIN
SELECT *
FROM useradds as ua
INNER JOIN household as hh on hh.idx_user_id = ua.idx_user_id
INNER JOIN house as h on h.idx_user_id = ua.idx_user_id
WHERE adddate >= (CURDATE() - INTERVAL inAddDate DAY) AND
((inLocation1 IS NULL OR downtown = inLocation1) OR
(inLocation2 IS NULL OR hwy = inLocation2) OR
(inLocation3 IS NULL OR dewey = inLocation3) OR
(inLocation4 IS NULL OR lewes = inLocation4))
ORDER BY ua.adddate DESC
LIMIT inStartItem, inRoomsPerPage;
END$$
每当我选择任何一个或多个位置时,这都可以正常工作,但如果我尝试跳过它则不起作用(它应该只选择最后一个 inAddDate 中的所有内容)。如果我更换
(inLocation1 IS NULL OR downtown = inLocation1)
和
downtown = COALESCE(NULLIF(inLocation1, ''), downtown))
然后如果我绕过该位置,它会选择所有内容,但如果我想搜索任何 2 个或更多位置,它不会返回任何内容。我相信一定有很好的方法来克服这个问题,因为我有更多的参数组要添加。