在sql中将返回值显示为列名

时间:2015-06-18 21:34:07

标签: mysql

我试图在mysql中运行查询以返回所有产品及其规格。我设法进行如下查询:

select `brands`.`name`,`products`.`reference`,`specifications`.`name`,`specs-product`.`value` 

from `specs-product` 

inner join `products` on `specs-product`.`product-id`=`products`.`product-id`
inner join `specifications` on `specs-product`.`specification-id`=`specifications`.`specification-id`  
inner join `brands` on `products`.`brand-id`=`brands`.`brand-id`

where 

`specs-product`.`product-id` in (1,2,3,4,5,6,7,8)

and

(
`specs-product`.`specification-id`='88' or 
`specs-product`.`specification-id`='103' or 
`specs-product`.`specification-id`='18' or 
`specs-product`.`specification-id`='15' or 
`specs-product`.`specification-id`='157' or 
`specs-product`.`specification-id`='89' or 
`specs-product`.`specification-id`='9' or 
`specs-product`.`specification-id`='223' or 
`specs-product`.`specification-id`='224' or 
`specs-product`.`specification-id`='29' or 
`specs-product`.`specification-id`='87' or 
`specs-product`.`specification-id`='219' or 
`specs-product`.`specification-id`='218' or 
`specs-product`.`specification-id`='220'
);

它返回所有值并正常工作。但是我试图显示产品的所有规格在一行中显示的值。因此实际上返回的结果只有8行。

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
  b.name AS brand,
  p.reference AS reference,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 88
      AND `product_id` = p.`product_id`
  ) AS weight,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 103
      AND `product_id` = p.`product_id`
  ) AS toneryield,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 18
      AND `product_id` = p.`product_id`
  ) AS stdpapercapacity,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 15
      AND `product_id` = p.`product_id`
  ) AS speed,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 157
      AND `product_id` = p.`product_id`
  ) AS resolution,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 89
      AND `product_id` = p.`product_id`
  ) AS powerreqs,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 9
      AND `product_id` = p.`product_id`
  ) AS maxmonthdutycycle,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 223
      AND `product_id` = p.`product_id`
  ) AS lowprice,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 224
      AND `product_id` = p.`product_id`
  ) AS highprice,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 29
      AND `product_id` = p.`product_id`
  ) AS documentfeeder,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 87
      AND `product_id` = p.`product_id`
  ) AS dimensions,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 219
      AND `product_id` = p.`product_id`
  ) AS colorimpressions,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 218
      AND `product_id` = p.`product_id`
  ) AS blackimpressions,
  (
    SELECT value
    FROM `specs-product`
    WHERE `specification_id` = 220
      AND `product_id` = p.`product_id`
  ) AS blacktonerinsqrfeet
FROM products p
INNER JOIN brands b ON b.`brand_id` = p.`brand_id`

答案 1 :(得分:0)

这不是关系操作,因此无法在SQL中完成(至少不能使用MySQL)。

但是,如果切换到MariaDB,您将能够使用名为CONNECT的存储引擎,可以使用(在许多其他方面)创建数据透视表。参见文档:

https://mariadb.com/kb/en/mariadb/connect-table-types-pivot-table-type/

答案 2 :(得分:0)

这很容易在mysql中完成。

Select group_concat(specifications.name)
..
..
Group by specs-product.product-id