SQL 复杂结构需要查询
SQL query needed for a complex structure
我有一个棘手的 SQL 查询需要构建以根据客户会话和地理 IP 数据获得最高优先级规则。
我附上了以下 tables:规则,rule_attribute,rule_attribute_value。
rule - table 存储所有规则的地方
Click here to see a screenshot of the 'rule' table
rule_attribute - table 存储所有规则属性的位置
Click here to see a screenshot of the 'rule_attribute' table
rule_attribute_value - table 存储所有规则属性值的位置
Click here to see a screenshot of the 'rule_attribute_value' table
当客户登录时,我可以访问所有这些属性(customer_id、customer_group_id、country_id、subdivision_one_id、subdivision_two_id)。只有 customer_id 和 customer_group_id 总是有值。其他的是可选的,但它们之间存在依赖关系。如果不首先选择一个国家,我们就不能进行细分。我们可以在不选择国家的情况下进行第二次细分,然后进行第一次细分。
我想得到的是最高优先级的规则,以最优化的方式匹配会话数据。我有一个涉及一些编码的解决方案,但我想看看是否可以直接通过 SQL.
以下是会话数据数组的一些示例:
Array
(
[customer_id] => 2
[customer_group_id] => 1
[current_store_id] => 0
[country_id] => 15
[subdivision_one_id] => 224
[subdivision_two_id] =>
)
Array
(
[customer_id] => 2
[customer_group_id] => 1
[current_store_id] => 0
[country_id] => 15
[subdivision_one_id] =>
[subdivision_two_id] =>
)
Array
(
[customer_id] => 3
[customer_group_id] => 2
[current_store_id] => 0
[country_id] =>
[subdivision_one_id] =>
[subdivision_two_id] =>
)
在没有更好地理解规则和数据的情况下,这是我能想到的最好的办法。它基于您的第一个数组示例 -
SELECT `r`.*
FROM `rule_attribute_value` `rav`
INNER JOIN `rule` `r`
ON `rav`.`rule_id` = `r`.`rule_id`
INNER JOIN `rule_attribute` `ra`
ON `rav`.`attribute_id` = `ra`.`attribute_id`
WHERE
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'customer' AND `rav`.`value` = 2) OR
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'customer_group' AND `rav`.`value` = 1) OR
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'country' AND `rav`.`value` = 15) OR
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'subdivision_one' AND `rav`.`value` = 224)
GROUP BY `r`.`rule_id`
HAVING COUNT(DISTINCT `rav`.`attribute_id`) = 4 /* 4 IS THE NUMBER OF ATTRIBUTES BEING QUERIED */
ORDER BY `r`.`position` ASC
LIMIT 1;
我有一个棘手的 SQL 查询需要构建以根据客户会话和地理 IP 数据获得最高优先级规则。
我附上了以下 tables:规则,rule_attribute,rule_attribute_value。
rule - table 存储所有规则的地方 Click here to see a screenshot of the 'rule' table
rule_attribute - table 存储所有规则属性的位置 Click here to see a screenshot of the 'rule_attribute' table
rule_attribute_value - table 存储所有规则属性值的位置 Click here to see a screenshot of the 'rule_attribute_value' table
当客户登录时,我可以访问所有这些属性(customer_id、customer_group_id、country_id、subdivision_one_id、subdivision_two_id)。只有 customer_id 和 customer_group_id 总是有值。其他的是可选的,但它们之间存在依赖关系。如果不首先选择一个国家,我们就不能进行细分。我们可以在不选择国家的情况下进行第二次细分,然后进行第一次细分。
我想得到的是最高优先级的规则,以最优化的方式匹配会话数据。我有一个涉及一些编码的解决方案,但我想看看是否可以直接通过 SQL.
以下是会话数据数组的一些示例:
Array
(
[customer_id] => 2
[customer_group_id] => 1
[current_store_id] => 0
[country_id] => 15
[subdivision_one_id] => 224
[subdivision_two_id] =>
)
Array
(
[customer_id] => 2
[customer_group_id] => 1
[current_store_id] => 0
[country_id] => 15
[subdivision_one_id] =>
[subdivision_two_id] =>
)
Array
(
[customer_id] => 3
[customer_group_id] => 2
[current_store_id] => 0
[country_id] =>
[subdivision_one_id] =>
[subdivision_two_id] =>
)
在没有更好地理解规则和数据的情况下,这是我能想到的最好的办法。它基于您的第一个数组示例 -
SELECT `r`.*
FROM `rule_attribute_value` `rav`
INNER JOIN `rule` `r`
ON `rav`.`rule_id` = `r`.`rule_id`
INNER JOIN `rule_attribute` `ra`
ON `rav`.`attribute_id` = `ra`.`attribute_id`
WHERE
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'customer' AND `rav`.`value` = 2) OR
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'customer_group' AND `rav`.`value` = 1) OR
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'country' AND `rav`.`value` = 15) OR
(`rav`.`store_id` = 0 AND `ra`.`attribute_code` = 'subdivision_one' AND `rav`.`value` = 224)
GROUP BY `r`.`rule_id`
HAVING COUNT(DISTINCT `rav`.`attribute_id`) = 4 /* 4 IS THE NUMBER OF ATTRIBUTES BEING QUERIED */
ORDER BY `r`.`position` ASC
LIMIT 1;