如何编写 SQL 语句来使用“LIKE”九个不同的值?
我必须选择所有记录,其中:
where FIELD2 like '%value21%'
or FIELD2 like '%value22%'
or FIELD2 like '%value23%'
-- repeat up to
or FIELD2 like '%value29%'
这里,value21
, ..., value29
是用户可以在提交查询之前放入表单中的参数。它们是(不是后续的)数字代码,而 FIELD2
是保存字符串值的数据库列。
哪种形式可以最紧凑地记录我的 SQL 查询?
注意:这与早期问题相关,但这需要LIKE
而不是等于。
I have to select all the records where:
where FIELD2 like '%value21%'
or FIELD2 like '%value22%'
or FIELD2 like '%value23%'
-- repeat up to
or FIELD2 like '%value29%'
Here, value21
, ..., value29
are parameters which the user can put in a form before submitting the query. They are (not subsequent) numerical codes, and FIELD2
is a database column holding a string value.
Which is the most compact form to write down my SQL query?
Note: This is related to an earlier question, but this needs LIKE
rather than equals.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕您会陷入困境:
至少在标准 SQL 中(您的特定引擎可能会提供某种形式的全文索引来提供帮助)。
像这样的查询通常表明数据库设计中存在标准化问题,多个值存储在单个字段值中。如果您的情况确实如此,并且您对数据库模式有一定程度的控制,我建议尽快解决问题。否则,请检查您的医疗保险,以确保其承保 SQL 引起的精神病 — 这可能会让您发疯。
I'm afraid you're stuck with:
at least in standard SQL (your particular engine might offer some form of full-text indexing that would help).
A query like this often indicates a normalization problem in your database design with multiple values stored in a single field value. If that's true in your case and you have any degree of control over the database schema, I advise fixing the problem as soon as possible. Otherwise check your medical coverage to make sure it covers SQL-induced psychosis — this can drive you crazy.
一种方式:
等等。
One way:
etc.