I have a query that returns multiple rows such as below in MySQL:
attribute_name value
----------------------------------------
username emailuser
domain mydomain.com
My Required output is as follows:
username domain
-----------------------------
emailuser mydomain.com
Current SQL:
SELECT pa2.attribute_name, upa2.value
FROM product p
INNER JOIN product_attribute pa ON p.product_id = pa.product_id
AND pa.attribute_name = 'alias'
INNER JOIN user_product_attribute upa ON pa.product_attribute_id upa.product_attribute_id
INNER JOIN user_product_attribute upa2 ON upa.user_product_id = upa2.user_product_id
INNER JOIN product_attribute pa2 ON pa2.product_attribute_id = upa2.product_attribute_id
AND pa2.attribute_name
IN (
'username', 'domain'
)
WHERE p.product_name = 'email'
AND upa.value = 'emailalias@domainalias'
I have been looking at group_concat, but i think that is the wrong path.. Any suggestions?
Nothing wrong with group_concat
. The following query will be slow but for a small table, that might not matter so much.
SELECT
GROUP_CONCAT(IF(`attribute_name`='username', `value`,'')) AS `username`,
GROUP_CONCAT(IF(`attribute_name`='domain', `value`, '')) AS `domain`,
FROM (your old table or the select query)
GROUP BY `attribute_name`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.