在MySQL表中搜索数字组合的性能有效方法

时间:2017-12-29 17:08:20

标签: mysql comparison combinations

假设我们在酒店房间里有所有可能的客人位置表。

这是我的表格(以及小提琴http://sqlfiddle.com/#!9/d01fa9

CREATE TABLE `Placement` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `people` smallint(3) unsigned DEFAULT NULL,
  `adults` smallint(3) unsigned DEFAULT NULL,
  `children` smallint(3) unsigned DEFAULT NULL,
  `child_1_from` smallint(3) unsigned DEFAULT NULL,
  `child_1_to` smallint(3) unsigned DEFAULT NULL,
  `child_2_from` smallint(3) unsigned DEFAULT NULL,
  `child_2_to` smallint(3) unsigned DEFAULT NULL,
  `child_3_from` smallint(3) unsigned DEFAULT NULL,
  `child_3_to` smallint(3) unsigned DEFAULT NULL,
  `provider_id` smallint(3) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
)

因此,我们希望根据用户的搜索找到确切的展示位置。搜索查询中最多可能有3个孩子。每个孩子都有一些年龄。

查看Placement表中的这一行:

people | adults | children | child_1_from | child_1_to | child_2_from | child_2_to | child_3_from | child_3_to

   4       1          3            12          15             9             11            3            6

所以,如果我想搜索1名成人,3名儿童和3名儿童和10名儿童,则按照给定的顺序比较第一,第二和第三个孩子的年龄范围

年龄顺序无关紧要时,我无法弄清楚如何查询数据库。因此,在给定的示例中,来自这3个数字的所有组合:[10,13,5],[13,10,5]等必须从数据库表中返回相同的行。

有什么建议吗?

0 个答案:

没有答案