First time asking a question on here, so apologizes if this ends up being formatted poorly.
I'm trying to pull some information from multiple tables in order to build a report. Said tables are coming from a Magento instance, in case that helps any.
So, I have four tables I'm working with: customer_entity, customer_address_entity, customer_address_entity_text, and customer_address_var_char
In customer_entity, I need to get three fields: entity_id, email, and group_id
In customer_address_entity, I need to get parent_id (which equals entity_id from above) and entity_id (which is not the same as above).
In customer_address_entity_text, I need entity_id, attribute_id, and value.
In customer_address_entity_varchar, I need value, entity_id, and attribute_id.
My issue mainly lies with the last table. I need to get value for six attribute ids, but I want to have value as multiple columns (first name, last name, email, etc) rather than returning as just value.
Here's my current code:
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)
This returns results formatted like so:
What I'd like is:
Yes, the column order isn't the greatest, but I'll deal with that after I get columns formatted as desired. I've tried a couple substring queries (one of which eventually crashed the server; oops) and using EXISTS. Yes, my code is hideous too; I plan on cleaning that up after I get the data I'm looking for in the format I'm looking for.
Thanks!
Edit: Should anyone want a copy of the code I ended up deploying (hat tip to 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