<tfoot id='b694o'></tfoot>

    1. <i id='b694o'><tr id='b694o'><dt id='b694o'><q id='b694o'><span id='b694o'><b id='b694o'><form id='b694o'><ins id='b694o'></ins><ul id='b694o'></ul><sub id='b694o'></sub></form><legend id='b694o'></legend><bdo id='b694o'><pre id='b694o'><center id='b694o'></center></pre></bdo></b><th id='b694o'></th></span></q></dt></tr></i><div id='b694o'><tfoot id='b694o'></tfoot><dl id='b694o'><fieldset id='b694o'></fieldset></dl></div>

      • <bdo id='b694o'></bdo><ul id='b694o'></ul>
      <legend id='b694o'><style id='b694o'><dir id='b694o'><q id='b694o'></q></dir></style></legend>
    2. <small id='b694o'></small><noframes id='b694o'>

      MySQL - 基于同一表中的行求和列值

      MySQL - sum column value(s) based on row from the same table(MySQL - 基于同一表中的行求和列值)
      <tfoot id='FDIRP'></tfoot>
        <bdo id='FDIRP'></bdo><ul id='FDIRP'></ul>

          • <i id='FDIRP'><tr id='FDIRP'><dt id='FDIRP'><q id='FDIRP'><span id='FDIRP'><b id='FDIRP'><form id='FDIRP'><ins id='FDIRP'></ins><ul id='FDIRP'></ul><sub id='FDIRP'></sub></form><legend id='FDIRP'></legend><bdo id='FDIRP'><pre id='FDIRP'><center id='FDIRP'></center></pre></bdo></b><th id='FDIRP'></th></span></q></dt></tr></i><div id='FDIRP'><tfoot id='FDIRP'></tfoot><dl id='FDIRP'><fieldset id='FDIRP'></fieldset></dl></div>

              <tbody id='FDIRP'></tbody>

            <small id='FDIRP'></small><noframes id='FDIRP'>

            <legend id='FDIRP'><style id='FDIRP'><dir id='FDIRP'><q id='FDIRP'></q></dir></style></legend>
                本文介绍了MySQL - 基于同一表中的行求和列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试根据同一表中的 ProductID 在新列中获取现金"、支票"和信用卡"总计.

                I'm trying to get 'Cash', 'Check' and 'Credit Card' totals in new columns based on ProductID from the same table.

                表格 - 付款

                +-----------+------------+---------------+--------+
                | ProductID |  SaleDate  | PaymentMethod | Amount |
                +-----------+------------+---------------+--------+
                |         3 | 2012-02-10 | Cash          |     10 |
                |         3 | 2012-02-10 | Cash          |     10 |
                |         3 | 2012-02-10 | Check         |     15 |
                |         3 | 2012-02-10 | Credit Card   |     25 |
                |         4 | 2012-02-10 | Cash          |      5 |
                |         4 | 2012-02-10 | Check         |      6 |
                |         4 | 2012-02-10 | Credit Card   |      7 |
                +-----------+------------+---------------+--------+
                

                期望的输出 -

                +------------+------+-------+-------------+-------+
                | ProductID  | Cash | Check | Credit Card | Total |
                +------------+------+-------+-------------+-------+
                |          3 |   20 |    15 |          25 |    60 |
                |          4 |    5 |     6 |           7 |    18 |
                +------------+------+-------+-------------+-------+
                

                我试过 LEFT JOINing 同一张桌子,但没有任何成功.任何建议,将不胜感激.谢谢.

                I've tried LEFT JOINing the same table but haven't had any success. Any suggestions would be appreciated. Thanks.

                不成功且不完整的尝试 -

                Unsuccessful and incomplete attempt -

                SELECT P.ProductID, Sum( PCash.Amount ) AS 'Cash', SUM( PCheck.Amount ) AS 'Check', SUM( PCredit.Amount) AS 'Credit Card' 
                FROM Payments AS P 
                LEFT JOIN Payments AS PCash ON P.ProductID = PCash.ProductID AND PCash.PaymentMethod = 'Cash'
                LEFT JOIN Payments AS PCheck ON P.ProductID = PCheck.ProductID AND PCheck.PaymentMethod = 'Check'
                LEFT JOIN Payments AS PCredit ON P.ProductID = PCredit.ProductID AND PCredit.PaymentMethod = 'Credit'
                WHERE P.SaleDate = '2012-02-10' GROUP BY ProductID;
                

                推荐答案

                我认为你让这件事变得比需要的更复杂.

                I think you're making this a bit more complicated than it needs to be.

                SELECT
                    ProductID,
                    SUM(IF(PaymentMethod = 'Cash', Amount, 0)) AS 'Cash',
                    -- snip
                    SUM(Amount) AS Total
                FROM
                    Payments
                WHERE
                    SaleDate = '2012-02-10'
                GROUP BY
                    ProductID
                

                这篇关于MySQL - 基于同一表中的行求和列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                <legend id='oqsNg'><style id='oqsNg'><dir id='oqsNg'><q id='oqsNg'></q></dir></style></legend>

                  <bdo id='oqsNg'></bdo><ul id='oqsNg'></ul>
                      <tfoot id='oqsNg'></tfoot>
                      <i id='oqsNg'><tr id='oqsNg'><dt id='oqsNg'><q id='oqsNg'><span id='oqsNg'><b id='oqsNg'><form id='oqsNg'><ins id='oqsNg'></ins><ul id='oqsNg'></ul><sub id='oqsNg'></sub></form><legend id='oqsNg'></legend><bdo id='oqsNg'><pre id='oqsNg'><center id='oqsNg'></center></pre></bdo></b><th id='oqsNg'></th></span></q></dt></tr></i><div id='oqsNg'><tfoot id='oqsNg'></tfoot><dl id='oqsNg'><fieldset id='oqsNg'></fieldset></dl></div>
                        <tbody id='oqsNg'></tbody>
                      • <small id='oqsNg'></small><noframes id='oqsNg'>