gpt4 book ai didi

php - mySQL:将一列的结果作为多列返回

转载 作者:可可西里 更新时间:2023-11-01 08:27:59 24 4
gpt4 key购买 nike

第一次在这里提问,如果格式不正确,敬请谅解。

我正在尝试从多个表中提取一些信息以构建报告。所述表格来自 Magento 实例,以防有帮助。

所以,我有四个正在使用的表:customer_entity、customer_address_entity、customer_address_entity_textcustomer_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 和值。

customer_address_entity_varchar 中,我需要值、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)

返回的结果格式如下:

Current

我想要的是:

Desired

是的,列顺序不是最好的,但我会在根据需要对列进行格式化后处理它。我尝试了几个子字符串查询(其中一个最终使服务器崩溃;哎呀)并使用了 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

最佳答案

不必多次加入表。您可以对结果进行分组并获得值。这是一个示例

两个表

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]>

关于php - mySQL:将一列的结果作为多列返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32999059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com