Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 管理数量未知的EAV属性上的联接_Php_Mysql - Fatal编程技术网

Php 管理数量未知的EAV属性上的联接

Php 管理数量未知的EAV属性上的联接,php,mysql,Php,Mysql,给定一个名为item的表,其中包含“主”记录,例如: 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 2

给定一个名为
item
的表,其中包含“主”记录,例如:

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
$attributes = ['size', 'color'];
还有另一个名为
custom_fields
的表,我认为它具有EAV结构(实体属性值):

对包含所有指定/配置属性的查询进行编码的最佳方法是什么,这些属性也将支持排序依据

事实:
1.数据结构无法更改。
2.属性未知(用户配置)。 3.在技术上,可以为给定类型的项目设置无限数量的“属性”(如上面的
大小
颜色
)(实际上,我无法想象超过50个)。
4.查询中包括/不包括哪些属性未知(用户配置)

假设指定的属性存在于数组中,例如:

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
$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}";
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'
将构造一个查询,该查询既可用于包含数据,也可用于允许ORDER BY。该查询类似于:

$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}";
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个
连接),可能会有性能问题

有没有更好的方法来组织这个?是否有一些方法(例如使用临时表)更有用或性能更好

更新:

MySQL的版本无法确保是任何特定的,因此这应该与任何版本兼容,因此请假设版本5.0

您也可以只使用一个**连接*来完成此操作,如下所示:

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)中项目id所在的行

选择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>

您也可以只使用一个**连接*来完成此操作,如下所示:

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)中项目id所在的行

选择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>

使用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"} |
+---------+------------+------------------------------------+-----------------------------------+
假设自定义\u字段具有唯一索引(项\u id,属性)

和a2.attribute='size'
是动态部分,您可以在其中定义要排序的属性,也可以在其中定义要提取的属性


它的行为与原始多连接查询略有不同。如果缺少一个或多个属性,它仍然返回该项。

< P>使用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"} |
+---------+------------+------------------------------------+-----------------------------------+
假设自定义\u字段具有唯一索引(项\u id,属性)

和a2.attribute='size'
是动态部分,您可以在其中定义要排序的属性,也可以在其中定义要提取的属性


它的行为与原始多连接查询略有不同。如果缺少一个或多个属性,它仍然返回该项。

有趣的方法。我正在跟踪它是如何工作的/它是做什么的。@cale_b-您也可以直接将第二条语句作为准备语句执行。我将在几分钟后用有趣的方法在我的答案末尾添加一个示例。我正在跟踪它是如何工作的/它是做什么的。@cale_b-您也可以直接将第二条语句作为准备语句执行。几分钟后,我会在我的答案末尾加上一个例子。使用这种方法,我将如何按照“颜色”进行排序?只是用一些解释更新了答案。要按颜色排序,您需要使用
和a2.attribute='color'
我刚刚实现了一个分布式软件包,我无法可靠地依赖mysql 5.7+的安装。糟糕,因为这是一个很酷的想法。好吧,对于mysql来说,这是一个有趣的想法。使用这种方法,我将如何按照“颜色”进行排序?只是用一些解释更新了答案。要按颜色排序,您需要使用
和a2.attribute='color'
我刚刚实现了一个分布式软件包,我无法可靠地依赖mysql 5.7+的安装。糟糕,因为这是一个很酷的主意。好吧,对于mysql来说