0

我有一个返回多行的查询,例如在 MySQL 中:

attribute_name  value
----------------------------------------
username          emailuser
domain          mydomain.com

我所需的输出如下:

username       domain
-----------------------------
emailuser     mydomain.com

当前 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'

我一直在看 group_concat,但我认为这是错误的路径.. 有什么建议吗?

4

1 回答 1

2

没有错group_concat。以下查询会很慢,但对于一个小表来说,这可能并不重要。

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` 
于 2011-02-09T14:23:26.020 回答