久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<legend id='jCVo8'><style id='jCVo8'><dir id='jCVo8'><q id='jCVo8'></q></dir></style></legend>

      • <bdo id='jCVo8'></bdo><ul id='jCVo8'></ul>

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

      1. <tfoot id='jCVo8'></tfoot>
        <i id='jCVo8'><tr id='jCVo8'><dt id='jCVo8'><q id='jCVo8'><span id='jCVo8'><b id='jCVo8'><form id='jCVo8'><ins id='jCVo8'></ins><ul id='jCVo8'></ul><sub id='jCVo8'></sub></form><legend id='jCVo8'></legend><bdo id='jCVo8'><pre id='jCVo8'><center id='jCVo8'></center></pre></bdo></b><th id='jCVo8'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='jCVo8'><tfoot id='jCVo8'></tfoot><dl id='jCVo8'><fieldset id='jCVo8'></fieldset></dl></div>
      2. MySQL 將行轉換為動態列數

        MySQL pivot row into dynamic number of columns(MySQL 將行轉換為動態列數)

          • <tfoot id='thXV4'></tfoot>

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

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

                <bdo id='thXV4'></bdo><ul id='thXV4'></ul>

                    <tbody id='thXV4'></tbody>
                • <legend id='thXV4'><style id='thXV4'><dir id='thXV4'><q id='thXV4'></q></dir></style></legend>
                  本文介紹了MySQL 將行轉換為動態列數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  假設我有三個不同的 MySQL 表:

                  Lets say I have three different MySQL tables:

                  表格products:

                  Table products:

                  id | name
                   1   Product A
                   2   Product B
                  

                  表格partners:

                  Table partners:

                  id | name
                   1   Partner A
                   2   Partner B
                  

                  表格sales:

                  partners_id | products_id
                            1             2
                            2             5
                            1             5
                            1             3
                            1             4
                            1             5
                            2             2
                            2             4
                            2             3
                            1             1
                  

                  我想要一張表格,行中包含合作伙伴,列中包含產品.到目前為止,我能夠得到這樣的輸出:

                  I would like to get a table with partners in the rows and products as columns. So far I was able to get an output like this:

                  name      | name      | COUNT( * )
                  Partner A   Product A          1
                  Partner A   Product B          1
                  Partner A   Product C          1
                  Partner A   Product D          1
                  Partner A   Product E          2
                  Partner B   Product B          1
                  Partner B   Product C          1
                  Partner B   Product D          1
                  Partner B   Product E          1
                  

                  使用此查詢:

                  SELECT partners.name, products.name, COUNT( * ) 
                  FROM sales
                  JOIN products ON sales.products_id = products.id
                  JOIN partners ON sales.partners_id = partners.id
                  GROUP BY sales.partners_id, sales.products_id
                  LIMIT 0 , 30
                  

                  但我想改為:

                  partner_name | Product A | Product B | Product C | Product D | Product E
                  Partner A              1           1           1           1           2
                  Partner B              0           1           1           1           1
                  

                  問題是我無法確定我將擁有多少產品,因此列號需要根據產品表中的行動態更改.

                  The problem is that I cannot tell how many products I will have so the column number needs to change dynamically depending on the rows in the products table.

                  這個很好的答案似乎不適用于 mysql:T-SQL 數據透視表?從行值創建表列的可能性

                  This very good answer does not seem to work with mysql: T-SQL Pivot? Possibility of creating table columns from row values

                  推薦答案

                  不幸的是,MySQL 沒有 PIVOT 功能,這基本上是您要嘗試執行的操作.因此,您需要使用帶有 CASE 語句的聚合函數:

                  Unfortunately MySQL does not have a PIVOT function which is basically what you are trying to do. So you will need to use an aggregate function with a CASE statement:

                  select pt.partner_name,
                    count(case when pd.product_name = 'Product A' THEN 1 END) ProductA,
                    count(case when pd.product_name = 'Product B' THEN 1 END) ProductB,
                    count(case when pd.product_name = 'Product C' THEN 1 END) ProductC,
                    count(case when pd.product_name = 'Product D' THEN 1 END) ProductD,
                    count(case when pd.product_name = 'Product E' THEN 1 END) ProductE
                  from partners pt
                  left join sales s
                    on pt.part_id = s.partner_id
                  left join products pd
                    on s.product_id = pd.prod_id
                  group by pt.partner_name
                  

                  參見 SQL 演示

                  由于您不知道產品,您可能希望動態執行此操作.這可以使用準備好的語句來完成.

                  Since you do not know the Products you will probably want to perform this dynamically. This can be done using prepared statements.

                  使用動態數據透視表(將行轉換為列),您的代碼將如下所示:

                  With dynamic pivot tables (transform rows to columns) your code would look like this:

                  SET @sql = NULL;
                  SELECT
                    GROUP_CONCAT(DISTINCT
                      CONCAT(
                        'count(case when Product_Name = ''',
                        Product_Name,
                        ''' then 1 end) AS ',
                        replace(Product_Name, ' ', '')
                      )
                    ) INTO @sql
                  from products;
                  
                  SET @sql = CONCAT('SELECT pt.partner_name, ', @sql, ' from partners pt
                  left join sales s
                    on pt.part_id = s.partner_id
                  left join products pd
                    on s.product_id = pd.prod_id
                  group by pt.partner_name');
                  
                  PREPARE stmt FROM @sql;
                  EXECUTE stmt;
                  DEALLOCATE PREPARE stmt;
                  

                  參見 SQL 演示

                  可能值得注意的是,GROUP_CONCAT 默認限制為 1024 字節.您可以通過在您的程序期間將其設置得更高來解決此問題,即.SET @@group_concat_max_len = 32000;

                  It's probably worth noting that GROUP_CONCAT is by default limited to 1024 bytes. You can work around this by setting it higher for the duration of your procedure, ie. SET @@group_concat_max_len = 32000;

                  這篇關于MySQL 將行轉換為動態列數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行)
                  reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結果;條款?)
                  Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數的 ignore 選項是忽略整個事務還是只是有問題的行?) - IT屋-程序員軟件開發技
                  Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環數組)
                  pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調用 o23.load 時發生錯誤 沒有合適的驅動程序)
                  How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數據庫表作為 Spark 數據幀讀取?)
                  <i id='5oUQf'><tr id='5oUQf'><dt id='5oUQf'><q id='5oUQf'><span id='5oUQf'><b id='5oUQf'><form id='5oUQf'><ins id='5oUQf'></ins><ul id='5oUQf'></ul><sub id='5oUQf'></sub></form><legend id='5oUQf'></legend><bdo id='5oUQf'><pre id='5oUQf'><center id='5oUQf'></center></pre></bdo></b><th id='5oUQf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='5oUQf'><tfoot id='5oUQf'></tfoot><dl id='5oUQf'><fieldset id='5oUQf'></fieldset></dl></div>

                      <bdo id='5oUQf'></bdo><ul id='5oUQf'></ul>

                        <tbody id='5oUQf'></tbody>

                            <legend id='5oUQf'><style id='5oUQf'><dir id='5oUQf'><q id='5oUQf'></q></dir></style></legend>

                            <tfoot id='5oUQf'></tfoot>

                            <small id='5oUQf'></small><noframes id='5oUQf'>

                            主站蜘蛛池模板: 午夜电影网 | 亚洲在线视频 | av福利网站 | 成人免费淫片aa视频免费 | 韩日一区 | 精品一区二区在线观看 | a网站在线观看 | 国产精品欧美一区二区三区 | 精品国产一区二区三区免费 | 在线日韩在线 | 国产精品久久久久久久久久久免费看 | 欧美1区 | 久久久人成影片免费观看 | 精品欧美一区二区三区久久久 | 午夜精品一区二区三区免费视频 | 国产成人久久精品一区二区三区 | 亚洲一区二区三区四区五区午夜 | 丁香色婷婷| 欧州一区二区三区 | jizz18国产| 国产区第一页 | 一区二区三区四区在线视频 | 亚洲一区二区三区四区五区午夜 | 成人三级在线播放 | 大伊人久久 | 成人精品高清 | 国产乱码一区 | 一级黄色裸片 | 91资源在线 | 亚洲一区二区精品视频 | 久久一区二区av | 国产在线高清 | 中文字幕亚洲视频 | 91精品国产99久久 | 一区视频 | 久久久69| 欧美天堂 | a毛片| 亚洲综合成人网 | 日韩精品一区二区三区 | 国产日韩久久 |