第一次在这里问一个问题,如果最终被格式化得不好,请道歉。
我试图从多个表中提取一些信息以构建报告。所述表格来自Magento实例,以防万一。
所以,我有四张桌子和我一起工作: customer_entity,customer_address_entity,customer_address_entity_text,和 customer_address_var_char
在 customer_entity 中,我需要获得三个字段:entity_id,email和group_id
在 customer_address_entity 中,我需要获取parent_id(等于上面的entity_id)和entity_id(与上面不同)。
在 customer_address_entity_text 中,我需要entity_id,attribute_id和value。
在 customer_address_entity_varchar 中,我需要value,entity_id和attribute_id。
我的问题主要在于最后一张表。我需要为六个属性ID获取值,但我希望将值作为多个列(名字,姓氏,电子邮件等),而不是仅返回值。
这是我目前的代码:
SELECT
customer_entity.entity_id as "Customer ID", email, customer_address_entity_text.value as "Street Address", customer_address_entity_varchar.value
FROM
customer_entity, customer_address_entity, customer_address_entity_text, customer_address_entity_varchar
WHERE
customer_entity.group_id="2"
AND
customer_entity.entity_id = customer_address_entity.parent_id
AND
customer_address_entity.entity_id = customer_address_entity_text.entity_id
AND
customer_address_entity_text.attribute_id="24"
AND
customer_address_entity.entity_id = customer_address_entity_varchar.entity_id
AND
customer_address_entity_varchar.attribute_id in (19,21,25,26,27)
返回格式如下的结果:
我想要的是:
是的,列顺序并不是最好的,但是在我按照需要格式化列后,我会处理它。我尝试了几个子字符串查询(其中一个最终崩溃了服务器; oops)并使用了EXISTS。是的,我的代码也很可怕;我计划以我正在寻找的格式查找数据后清理它。
谢谢!
编辑:是否有人想要我最终部署的代码副本(帽子提示给Bernd):
SELECT
e.entity_id as "Customer ID", e.email, t.value AS "Street Address",
GROUP_CONCAT(IF(v.attribute_id = 19,v.value,NULL)) AS "First Name",
GROUP_CONCAT(IF(v.attribute_id = 21,v.value,NULL)) AS "Last Name",
GROUP_CONCAT(IF(v.attribute_id = 25,v.value,NULL)) AS "City",
GROUP_CONCAT(IF(v.attribute_id = 27,v.value,NULL)) AS "Region/State",
GROUP_CONCAT(IF(v.attribute_id = 26,v.value,NULL)) AS "Country"
FROM customer_entity e
LEFT JOIN customer_address_entity a ON a.parent_id = e.entity_id
LEFT JOIN customer_address_entity_varchar v ON v.entity_id = a.entity_id
LEFT JOIN customer_address_entity_text t on t.entity_id = a.entity_id
WHERE e.group_id = 2
AND t.attribute_id = 24
GROUP BY v.entity_id
答案 0 :(得分:2)
没有必要多次加入表格。您可以对结果进行分组并获取值。这是一个样本
两个表
MariaDB [tmp]> select *from names;
+----+-------+----------------+
| id | name | email |
+----+-------+----------------+
| 1 | Bernd | bernd@bernd.de |
| 2 | David | david@david.de |
+----+-------+----------------+
2 rows in set (0.00 sec)
MariaDB [tmp]> select * from customer_address_entity;
+-----------+--------------+------------------+
| entity_id | attribute_id | value |
+-----------+--------------+------------------+
| 1 | 1 | Duesseldorf |
| 1 | 2 | 40211 |
| 1 | 3 | berlinerplatz 55 |
| 1 | 4 | 0211 / 1234567 |
| 2 | 1 | Bremen |
| 2 | 2 | 21334 |
| 2 | 3 | Aachenerstr. 99 |
| 2 | 4 | 0432 / 7890111 |
+-----------+--------------+------------------+
8 rows in set (0.00 sec)
加入表格
MariaDB [tmp]> SELECT
-> n.*,a.*
-> FROM `names` n
-> LEFT JOIN customer_address_entity a ON a.entity_id = n.id;
+----+-------+----------------+-----------+--------------+------------------+
| id | name | email | entity_id | attribute_id | value |
+----+-------+----------------+-----------+--------------+------------------+
| 1 | Bernd | bernd@bernd.de | 1 | 1 | Duesseldorf |
| 1 | Bernd | bernd@bernd.de | 1 | 2 | 40211 |
| 1 | Bernd | bernd@bernd.de | 1 | 3 | berlinerplatz 55 |
| 1 | Bernd | bernd@bernd.de | 1 | 4 | 0211 / 1234567 |
| 2 | David | david@david.de | 2 | 1 | Bremen |
| 2 | David | david@david.de | 2 | 2 | 21334 |
| 2 | David | david@david.de | 2 | 3 | Aachenerstr. 99 |
| 2 | David | david@david.de | 2 | 4 | 0432 / 7890111 |
+----+-------+----------------+-----------+--------------+------------------+
8 rows in set (0.00 sec)
将其分组并获取字段
MariaDB [tmp]> SELECT
-> n.name, n.email,
-> GROUP_CONCAT( IF ( a.attribute_id = 1 , a.value,NULL)) AS city,
-> GROUP_CONCAT( IF ( a.attribute_id = 2 , a.value,NULL)) AS plz,
-> GROUP_CONCAT( IF ( a.attribute_id = 3 , a.value,NULL)) AS street,
-> GROUP_CONCAT( IF ( a.attribute_id = 4 , a.value,NULL)) AS phone
-> FROM `names` n
-> LEFT JOIN customer_address_entity a ON a.entity_id = n.id
-> GROUP BY a.entity_id;
+-------+----------------+-------------+-------+------------------+----------------+
| name | email | city | plz | street | phone |
+-------+----------------+-------------+-------+------------------+----------------+
| Bernd | bernd@bernd.de | Duesseldorf | 40211 | berlinerplatz 55 | 0211 / 1234567 |
| David | david@david.de | Bremen | 21334 | Aachenerstr. 99 | 0432 / 7890111 |
+-------+----------------+-------------+-------+------------------+----------------+
2 rows in set (0.00 sec)
MariaDB [tmp]>
答案 1 :(得分:0)
MySQL支持连接表本身,因此您需要多次连接eav表并提取' value'领域: (实施例)
MyOtherType