mysql - 连接两个表加上数据透视表作为存储过程 - MySQL

标签 mysql stored-procedures mysql-workbench

我试图在 MySQL 上做一个订单过程来制作一个数据透视表,但是它真的很复杂,我不知道该怎么做,我不知道是否有其他方法可以做起来更容易。

我需要获取在特定地址购买了多少产品,GROUP_CONCAT(ship_addr + ship_zip + ship_contry) 的结果。

订单表 - 包含订单地址。

+----+----------------+----------+--------------+
| id |   ship_addr    | ship_zip | ship_country |
+----+----------------+----------+--------------+
|  1 | Addr1          |  123     |           DE |
|  2 | Addr2          |  321     |           DE |
|  3 | Addr1          |  123     |           DE |
|  4 | Addr2          |  321     |           DE |
|  5 | Addr3          |  543     |           DE |
+----+----------------+----------+--------------+

订单持仓表

+----+---------------+------------+--------------+
| id |   order_id    | product_id | quantity     |
+----+----------------+-----------+--------------+
|  1 | 1             |  1         |            5 |
|  2 | 1             |  2         |           10 |
|  3 | 2             |  3         |            5 |
|  4 | 3             |  1         |            5 |
|  5 | 4             |  1         |           10 |
|  6 | 5             |  1         |           10 |
+----+----------------+----------+---------------+

预期结果,列标题为product_id:

+-----------------+-------+-------+-------+
|  Address        |   1   |   2   |   3   | 
+-----------------+-------+-------+-------+
|  Addr1, 123, DE | 10    |  10   |     0 |
|  Addr2, 321, DE | 10    |  0    |     5 |
|  Addr3, 543, DE | 10    |  0    |     0 |
+-----------------+-------+-------+-------+

我无法复制我尝试过的存储过程,因为它们完全失败了。

最佳答案

您好,我想出了这个解决方案,只是缺少一个部分来用零替换 null 。当我热更多时间时,我会尝试,但结果符合您的期望。也附上输出。这里值得一提的是,pivoted column需要静态添加ie(product_id)。如果需要,您也可以创建一个动态查询来容纳这些旋转列。 因为它是用 sql-server 标记的,所以我的答案来自 SQL Server 语法。

        declare @order as table (
        id int , ship_addr varchar(100),ship_zip varchar(100) , ship_country varchar(100)
        )

        insert into @order (id,ship_addr,ship_zip,ship_country) values (1,'Add1','123','DE')
        insert into @order (id,ship_addr,ship_zip,ship_country) values (2,'Add2','321','DE')
        insert into @order (id,ship_addr,ship_zip,ship_country) values (3,'Add1','123','DE')
        insert into @order (id,ship_addr,ship_zip,ship_country) values (4,'Add2','321','DE')
        insert into @order (id,ship_addr,ship_zip,ship_country) values (5,'Add3','543','DE')

        declare @orderposition as table (
        id int , order_id int ,product_id int , quantity int
        )

        insert into @orderposition (id,order_id,product_id,quantity) values (1,1,1,5)
        insert into @orderposition (id,order_id,product_id,quantity) values (2,1,2,10)
        insert into @orderposition (id,order_id,product_id,quantity) values (3,2,3,5)
        insert into @orderposition (id,order_id,product_id,quantity) values (4,3,1,5)
        insert into @orderposition (id,order_id,product_id,quantity) values (5,4,1,10)
        insert into @orderposition (id,order_id,product_id,quantity) values (6,5,1,10)

        select  pvt.fulladd as Address , ISNULL([1],0) as [1] , ISNULL([2],0) as [2] , ISNULL([3],0) as [3]  from 
        (select fulladd, product_id , sum( isnull( quantity,0)) as qty from (
        select (o.ship_addr +' , ' + o.ship_zip +' ,' + o.ship_country) as fulladd , o.id from @order o
        ) as o
        inner join @orderposition p on p.order_id= o.id
        group by  fulladd, p.product_id
        ) as final



        pivot 
        (
           max(qty)  for product_id in ([1],[2],[3])

        ) as pvt

enter image description here

关于mysql - 连接两个表加上数据透视表作为存储过程 - MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157770/

相关文章:

java - Spring boot 无法从数据源(mysql)确定 jdbc url

php - 获取通知每当 MySQL 表在 PHP 中更新时没有任何刷新并且服务器上没有太多负载

mysql - 调用存储过程将 NULL 插入所有列 MySQL

mysql - 如何在 MySQL Workbench 的一个 Pane 中写出调试消息?

mysql持续时间和获取时间

mysql - 如何编写以下 mySQL 查询?

php - Yii 不验证 2 列的唯一索引

oracle - 在perl脚本中调用带有输入参数和输出光标的存储过程

oracle - 使 PL/SQL 存储过程在发生异常时回滚所有更改?

Mysql2::错误:表不存在:显示来自 `sessions` 的完整字段