sql - 使用多个 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?
解决方案
您对该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 ?)
推荐阅读
- animation - 向现有 mixamo FBX 模型 three.js 添加不同的动画
- node.js - 是否可以使用 firebase 云消息传递和 node.js 服务器仅向活动客户端发送推送通知
- html - 为什么 flex 元素的高度没有逐渐过渡?
- python - 在 Pandas 中,如何快速将数字列转换为离散值?
- sendgrid - 来自 SendGrid 的确认电子邮件以垃圾邮件告终
- ssh - 无法通过 SSH 保存到 crontab,但在本地登录时可以
- react-native - TabOneScreen({ navigation }) 的导航类型是什么?
- wordpress - WooCommerce 扩展条款和条件框
- java - 转换 2 Exact Pojos,但以通用方式使用不同的命名空间
- database - NoSQL 新手,对创建集合有点困惑