6

通过编写以下查询

SELECT item_name, YEAR( DATE ) , SUM( item_sold_qty ) 
FROM item
JOIN sales ON item.id = sales.item_number
GROUP BY YEAR( DATE ) , item_name
ORDER BY item_name

我能够得到以下结果

item_name   YEAR( DATE )    SUM( item_sold_qty )
pencil          2011              22
pencil          2012              44
eraser          2012              22
eraser          2011              11
pen             2012              66
pen             2011              33
nib             2012              88
nib             2011              44

相反,我希望通过以下方式获得结果

 item_name      2011    2012
    pencil       22      44            
    eraser       11      22                   
    pen          33      66                  
    nib          44      88

我不太擅长 sql,也不知道如何将年份设置为列名。需要帮忙。

注意 :: 我的数据库有 2 个表。销售表的日期列具有不同的日期,如 2012-03-01、2012-04-02、2011-07-03 等...

4

3 回答 3

5

也许是这样的:

SELECT 
    item_name, 
    SUM(CASE WHEN YEAR( DATE )=2011 THEN item_sold_qty ELSE 0 END) AS '2011',
    SUM(CASE WHEN YEAR( DATE )=2012 THEN item_sold_qty ELSE 0 END) AS '2012'
FROM 
    item
JOIN sales ON item.id = sales.item_number
GROUP BY
    item_name
ORDER BY 
    item_name

编辑

如果你想要其他年份并且仍然总结它们。然后你可以这样做:

SELECT 
    item_name, 
    SUM(CASE WHEN YEAR( DATE )=2011 THEN item_sold_qty ELSE 0 END) AS '2011',
    SUM(CASE WHEN YEAR( DATE )=2012 THEN item_sold_qty ELSE 0 END) AS '2012',
    SUM(CASE WHEN NOT YEAR( DATE ) IN (2011,2012) THEN item_sold_qty ELSE 0 END) AS 'AllOtherYears'
FROM 
    item
JOIN sales ON item.id = sales.item_number
GROUP BY
    item_name
ORDER BY 
    item_name

编辑2

如果你有很多年并且你不想继续增加年数。然后你需要使用动态sql。这意味着您连接 sql 的 varchar 然后执行它。

有用的参考资料:

于 2012-04-26T09:46:24.647 回答
1

上面的答案会起作用,但每年添加一个案例可能合适,也可能不合适。如果您在 PHP 平台上,您可以像这样更改现有数组的布局

foreach($items as $item)
{
    $item_names[$item[item_name]][$item[year]] += $item[item_sold_qty];
}

这将创建一个数组,如下所示:

Array
(
    [pencil] => Array
        (
            [2011] => 22
            [2012] => 44
        )

    [eraser] => Array
        (
            [2012] => 22
            [2011] => 11
        )

    [pen] => Array
        (
            [2012] => 66
            [2011] => 33
        )

    [nib] => Array
        (
            [2012] => 88
            [2011] => 44
        )

)

还要更改您的原始查询以为变量分配更好的名称:

SELECT item_name, YEAR( DATE ) as year , SUM( item_sold_qty ) as item_sold_qty
FROM item
JOIN sales ON item.id = sales.item_number
GROUP BY YEAR( DATE ) , item_name
ORDER BY item_name

以上假设您的 mysql 结果数组如下所示:

Array
(
    [0] => Array
        (
            [item_name] => pencil
            [year] => 2011
            [item_sold_qty] => 22
        )

    [1] => Array
        (
            [item_name] => pencil
            [year] => 2012
            [item_sold_qty] => 44
        )

    [2] => Array
        (
            [item_name] => eraser
            [year] => 2012
            [item_sold_qty] => 22
        )

    [3] => Array
        (
            [item_name] => eraser
            [year] => 2011
            [item_sold_qty] => 11
        )

    [4] => Array
        (
            [item_name] => pen
            [year] => 2012
            [item_sold_qty] => 66
        )

    [5] => Array
        (
            [item_name] => pen
            [year] => 2011
            [item_sold_qty] => 33
        )

    [6] => Array
        (
            [item_name] => nib
            [year] => 2012
            [item_sold_qty] => 88
        )

    [7] => Array
        (
            [item_name] => nib
            [year] => 2011
            [item_sold_qty] => 44
        )

)
于 2012-04-26T09:58:04.583 回答
0

这里:

SELECT
    i.item_name, 
    SUM(s1.item_sold_qty) AS '2011',
    SUM(s2.item_sold_qty) AS '2012'

FROM item i
     LEFT JOIN sales s1 ON (i.id = s1.item_number AND YEAR(s1.DATE) = 2011)
     LEFT JOIN sales s2 ON (i.id = s2.item_number AND YEAR(s2.DATE) = 2012)

GROUP BY i.item_name
ORDER BY i.item_name

当然,您必须添加尽可能多的连接,因为您想搜索多少年......

于 2012-04-26T10:04:42.170 回答