SQL select中的重复ID

时间:2017-12-14 13:14:24

标签: sql

mysql中的select是这样的:

SELECT v.product_id, 
       nome            AS Produto, 
       presentation    AS Descricao, 
       presentation    AS Descricao2, 
       name1           AS categoria, 
       description_two AS descricao, 
       price           AS preco, 
       quantity        AS estoque, 
       width, 
       height, 
       depth, 
       weight, 
       name, 
       referencecode, 
       datapostagem 
FROM   variant v 
       INNER JOIN productcategory p 
               ON v.product_id = p.product_id 
       INNER JOIN product 
               ON product.id = p.product_id 
       INNER JOIN category 
               ON category.id = category_id 
       INNER JOIN image i 
               ON i.product_id = p.product_id 
       INNER JOIN descriptiongroup D 
               ON D.product_id = p.product_id 
       INNER JOIN stock S 
               ON S.variant_id = v.id 
       INNER JOIN dimensiongroup G 
               ON G.variant_id = v.id 
LIMIT  10 

结果返回大量重复的product_id,如何在另一列中分隔product_id?像product2,product3,product4?

如果我这样做,选择它会显示正确的表格,但只有一个结果,我如何在主要选择中加入以下选择?

 select 
     product_id,
     sum(case when presentation = 'Unitário = R$ 11,90 ' THEN price END) product_id1,
     sum(case when presentation = '5 Peças = R$ 5,00 cada' THEN price END) product_id2,
     sum(case when presentation = 'Bluesky Todas' THEN price END) product_id3
 from 
     Variant
 where 
     product_id = 1604

结果是:

        product_id  product_id1     product_id2     product_id3     
        1604    11.9    25  15

如何让它变得动态,向我展示不仅仅是一个结果?

以下结果的图像:

enter image description here

1 个答案:

答案 0 :(得分:0)

正如上面的评论所述,遵循https://stackoverflow.com/help/mcve中的指南可以帮助其他人帮助您。

我可以根据您告诉我们的内容提出建议:这是问题:您有行中的产品ID,并希望在列中看到它们。

Excel做得很好,因为它主要是一种演示工具。 Tableau和Qlik也是这样做的。最简单的方法是选择您想要的数据(如您所做),将其复制到演示工具中并在那里进行交叉选项卡。 MySQL,像其他可靠的关系数据库系统一样管理数据;你应该使用某种演示工具来呈现它。

某些数据库(例如PostgreSQL和Microsoft SQL Server)在其命令行工具中具有有限的交叉表功能,但我从不使用它们。

SQL中的数据透视表是可能的,但实际上很笨拙,因为这不是SQL的用途。您可以在http://www.thedatastudio.net/ugly_cross_tab.htm找到更多相关信息。

底线是:改为使用演示工具。