首页 > 解决方案 > 使用多个 OR LIKE 语句

问题描述

我有一个数据库,我想过滤一些条目。这是过滤器的样子:

SELECT * FROM Item WHERE 1=1  AND Favorite = 1 AND (  DocType = ?  OR  DocType = ?  OR  DocType = ?  OR  DocType = ? )  AND (Title Like ? OR Location LIKE ? OR DocType LIKE ? OR ProType LIKE ?)

过滤器几乎可以工作。我想专注于最后一部分:

AND (Title Like ? OR Location LIKE ? OR DocType LIKE ? OR ProType LIKE ?)

因为好像

 OR DocType LIKE ? OR ProType LIKE ?

被忽略。例如,当使用 DocType 切换 Location 时:

AND (Title Like ? OR DocType LIKE ? OR Location LIKE ? OR ProType LIKE ?)

我可以找到与 DocType 相关的任何内容。无法再找到位置(就像 ProType 一样)。所以我想我写错了。

我需要如何将其写下来以便过滤 Title、Location、DocType 和 ProType?

标签: sqlfilter

解决方案


您对该DocType列有两项检查,一项与 = 一项与一项与 LIKE 并且由于您已将 % 添加到您的搜索字符串中,因此 = 很可能会返回 false 因此不会返回任何内容,因为这两项检查与 AND 分组,因此您的 WHERE 条件将成为(缩短)

WHERE DocType = '%ABC%' AND DocType LIKE '%ABC'

我认为您应该在查询中直接删除 AND 之前的部分

WHERE 1=1 AND Favorite = 1 AND (Title Like ? OR Location LIKE ? OR DocType LIKE ? OR ProType LIKE ?)

推荐阅读