使用条件列表的任何子集查询数据库
我有一个包含许多列的 MySQL 数据库(例如,column0 ..column19)。
我想选择数据库中满足以下形式条件的所有行:
column0 < value0 AND column1 < value1 AND ... AND column19 < value19.
问题是我想选择满足上述任意 15 个条件的行。
我知道我可以对 15 个条件的所有可能组合执行多个查询,但我正在寻找仅包含 1 个查询的解决方案。
用MySQL可以吗?是否有另一种数据库架构允许此类查询?
谢谢
I have a MySQL database with a number of columns (say column0 .. column19).
I want to select all rows of the database which satisfy a condition of the form:
column0 < value0 AND column1 < value1 AND ... AND column19 < value19.
The catch is that I want to select rows which satisfy ANY 15 conditions out of the above.
I know I can do multiple queries will all possible combinations of 15 conditions, but I'm looking for a solution with only 1 query.
Is it possible with MySQL? Is there another database architecture which allows such queries?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下内容,但不要指望它会表现得特别好!
You could use the below, but don't expect it'll perform particularly well!
Will的答案也可以写成:
上面的内容仅在MYSQL中运行,不适用于其他数据库。因此,我认为使用标准
CASE WHEN
比这更好。处理此类查询的另一种方法如下(
id
是表的主键):如果有 20 个索引(每个
column0
一个,column1
, ...,column19
),可能会更快。Will's answer can also be written as:
The above will work only in MYSQL, not other databases. So, I suppose it's better to use the standard
CASE WHEN
than this.Another way to handle such a query would be like this (
id
is the primary key of the table):If there are 20 indexes (one on every
column0
,column1
, ...,column19
), it might, might be quicker.