给定一个名为item
的表,其中包含" master"其中的记录,例如:
item_id item_type item_name item_description
1 clothing T-Shirt Get your t-shirt here
2 clothing Polo Shirt Another kind of short-sleeve shirt
3 computer Macbook 2016 Macbook Pro with retina
另一个名为custom_fields
的表格,我相信它将被称为EAV结构(实体 - 属性 - 值):
id item_id attribute value
1 1 size large
2 1 color purple
3 2 size medium
4 2 color green
5 3 memory 16GB
6 3 hard drive 512GB SSD
对包含所有指定/已配置属性的查询进行编码的最佳方法是什么?还支持 ORDER BY
?
事实:
1.数据结构不能改变
2.属性未知(用户配置)。
3.技术上有无限数量的"属性" (例如,上面的size
或color
)可能针对特定类型的项目设置(实际上,我无法想象超过50个)。
4.查询中包含哪些属性未知(用户配置)。
假设指定的属性存在于数组中,例如:
$attributes = ['size', 'color'];
似乎某些PHP做了类似的事情:
$counter = 1;
$from = 'item AS i';
$fields = 'name, description';
$order_by = 'size';
foreach( $attributes AS $attribute ) {
$table_alias = "a{$counter}";
$fields.= ",{$table_alias}.value AS {$attribute}";
$from.= " INNER JOIN custom_fields AS {$table_alias} ON i.item_id = {$table_alias}.item_id AND {$table_alias}.attribute = '{$attribute}'";
$counter++;
}
$query = "SELECT {$fields} FROM {$from} {$where} ORDER BY {$order_by}";
构造一个可用于包含数据的查询,以及允许ORDER BY。该查询看起来像:
SELECT name, description, a1.value AS size, a2.value AS color
FROM item AS i
INNER JOIN custom_fields AS a1 ON i.item_id = a1.item_id
AND a1.attribute = 'size'
INNER JOIN custom_fields AS a2 ON i.item_id = a2.item_id
AND a2.attribute = 'color'
但是,如果有20多列,我担心这个查询似乎很讨厌(20 JOINS
)并且可能会出现性能问题。
有没有更好的方法来构建它?是否有一些方法,比如使用临时表,可能更有用或更高效?
更新
MySQL的版本不能确保任何特定的,所以这应该与任何版本兼容,所以请假设版本5.0 。
答案 0 :(得分:1)
你也可以只用一个** JOIN *这样做:
SELECT m.*
,GROUP_CONCAT(IF(cf.attribute = 'color', cf.`value`, NULL)) AS 'color'
,GROUP_CONCAT(IF(cf.attribute = 'size', cf.`value`, NULL)) AS 'size'
,GROUP_CONCAT(IF(cf.attribute = 'memory', cf.`value`, NULL)) AS 'memory'
,GROUP_CONCAT(IF(cf.attribute = 'hard drive', cf.`value`, NULL)) AS 'hard drive'
FROM master m
LEFT JOIN custom_fields cf ON m.item_id = cf.item_id
GROUP BY m.item_id;
要为产品(或更多)生成查询,您可以使用以下命令: 对于所有产品,删除(1);
中的 WHERE item_id行SELECT m。*
select DISTINCT
CONCAT("SELECT m.*\n"
, GROUP_CONCAT(",GROUP_CONCAT(IF(cf.attribute = '",attribute,"', cf.`value`, NULL)) AS '",attribute,"'" SEPARATOR '\n')
, "FROM master m\nLEFT JOIN custom_fields cf ON m.item_id = cf.item_id\nGROUP BY m.item_id") as myquery
FROM custom_fields
WHERE item_id in (1);
<强>样品强>
mysql> select DISTINCT
-> CONCAT("SELECT m.*\n"
-> , GROUP_CONCAT(",GROUP_CONCAT(IF(cf.attribute = '",attribute,"', cf.`value`, NULL)) AS '",attribute,"'" SEPARATOR '\n')
-> , "FROM master m\nLEFT JOIN custom_fields cf ON m.item_id = cf.item_id\nGROUP BY m.item_id") as myquery
-> FROM custom_fields;
<强>结果强>
SELECT m.*
,GROUP_CONCAT(IF(cf.attribute = 'size', cf.`value`, NULL)) AS 'size'
,GROUP_CONCAT(IF(cf.attribute = 'color', cf.`value`, NULL)) AS 'color'
,GROUP_CONCAT(IF(cf.attribute = 'size', cf.`value`, NULL)) AS 'size'
,GROUP_CONCAT(IF(cf.attribute = 'color', cf.`value`, NULL)) AS 'color'
,GROUP_CONCAT(IF(cf.attribute = 'memory', cf.`value`, NULL)) AS 'memory'
,GROUP_CONCAT(IF(cf.attribute = 'hard drive', cf.`value`, NULL)) AS 'hard drive'FROM master m
LEFT JOIN custom_fields cf ON m.item_id = cf.item_id
GROUP BY m.item_id
1 row in set (0,00 sec)
mysql>
并执行此查询
mysql> SELECT m.*
-> ,GROUP_CONCAT(IF(cf.attribute = 'size', cf.`value`, NULL)) AS 'size'
-> ,GROUP_CONCAT(IF(cf.attribute = 'color', cf.`value`, NULL)) AS 'color'
-> ,GROUP_CONCAT(IF(cf.attribute = 'size', cf.`value`, NULL)) AS 'size'
-> ,GROUP_CONCAT(IF(cf.attribute = 'color', cf.`value`, NULL)) AS 'color'
-> ,GROUP_CONCAT(IF(cf.attribute = 'memory', cf.`value`, NULL)) AS 'memory'
-> ,GROUP_CONCAT(IF(cf.attribute = 'hard drive', cf.`value`, NULL)) AS 'hard drive'FROM master m
-> LEFT JOIN custom_fields cf ON m.item_id = cf.item_id
-> GROUP BY m.item_id ;
+---------+-----------+------------+------------------------------------+--------+--------+--------+--------+--------+------------+
| item_id | item_type | item_name | item_description | size | color | size | color | memory | hard drive |
+---------+-----------+------------+------------------------------------+--------+--------+--------+--------+--------+------------+
| 1 | clothing | T-Shirt | Get your t-shirt here | large | purple | large | purple | NULL | NULL |
| 2 | clothing | Polo Shirt | Another kind of short-sleeve shirt | medium | green | medium | green | NULL | NULL |
| 3 | computer | macbook | 2016 Macbook Pro with retina | NULL | NULL | NULL | NULL | 16GB | 512GB SSD |
+---------+-----------+------------+------------------------------------+--------+--------+--------+--------+--------+------------+
3 rows in set (0,00 sec)
mysql>
具有预付款声明的样本
mysql> select DISTINCT
-> CONCAT("SELECT m.*\n"
-> , GROUP_CONCAT(",GROUP_CONCAT(IF(cf.attribute = '",attribute,"', cf.`value`, NULL)) AS '",attribute,"'" SEPARATOR '\n')
-> , "FROM master m\nLEFT JOIN custom_fields cf ON m.item_id = cf.item_id\nGROUP BY m.item_id") as myquery INTO @sql
-> FROM custom_fields;
Query OK, 1 row affected (0,00 sec)
mysql> PREPARE stmt FROM @sql;
Query OK, 0 rows affected (0,01 sec)
Statement prepared
mysql> execute stmt;
+---------+-----------+------------+------------------------------------+--------+--------+--------+--------+--------+------------+
| item_id | item_type | item_name | item_description | size | color | size | color | memory | hard drive |
+---------+-----------+------------+------------------------------------+--------+--------+--------+--------+--------+------------+
| 1 | clothing | T-Shirt | Get your t-shirt here | large | purple | large | purple | NULL | NULL |
| 2 | clothing | Polo Shirt | Another kind of short-sleeve shirt | medium | green | medium | green | NULL | NULL |
| 3 | computer | macbook | 2016 Macbook Pro with retina | NULL | NULL | NULL | NULL | 16GB | 512GB SSD |
+---------+-----------+------------+------------------------------------+--------+--------+--------+--------+--------+------------+
3 rows in set (0,00 sec)
mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0,00 sec)
mysql>
答案 1 :(得分:1)
使用mysql 5.7,您可以考虑将属性检索为json:
SELECT
i.item_id,
i.item_name,
i.item_description,
CONCAT('{',
GROUP_CONCAT(CONCAT(JSON_QUOTE(a1.attribute),
':',
JSON_QUOTE(a1.value))
SEPARATOR ','),
'}') AS att
FROM
item AS i
INNER JOIN
custom_fields AS a1 ON i.item_id = a1.item_id
INNER JOIN
custom_fields AS a2 ON i.item_id = a2.item_id
AND a2.attribute = 'size'
WHERE
a1.attribute IN ('color' , 'size')
GROUP BY 1
ORDER BY a2.value DESC
结果:
+---------+------------+------------------------------------+-----------------------------------+
| item_id | item_name | item_description | att |
+---------+------------+------------------------------------+-----------------------------------+
| 2 | Polo Shirt | Another kind of short-sleeve shirt | {"size":"medium","color":"green"} |
| 1 | T-Shirt | Get your t-shirt here | {"size":"large","color":"purple"} |
+---------+------------+------------------------------------+-----------------------------------+
假设custom_fields具有唯一索引(item_id,属性)。
AND a2.attribute = 'size'
是动态部分,您可以在其中定义要按顺序排列的属性,以及定义要提取的属性的a1.attribute IN ('color' , 'size')
。
它与原始多连接查询略有不同。如果缺少一个或多个属性,它仍会返回该项目。