【问题标题】:how to make a apriori table with dynamicaly mysql如何使用动态mysql制作先验表
【发布时间】:2020-07-07 08:40:56
【问题描述】:

我有 3 个这样的表

create table order_match
(
id int(10) PRIMARY KEY not null,
order_status_id int(10) not null
);

create table order_match_detail
(
 id int(10) PRIMARY KEY not null,
 order_match_id int(10) not null,
 product_id int(10) NOT NULL
);

create table product
(
id int(10) PRIMARY KEY not null,
name varchar(255) not null
);

Insert into order_match (id, order_status_id)
select 1, 6 union all
select 2, 7 union all
select 3, 6 union all
select 4, 6;

Insert into order_match_detail (id, order_match_id, product_id)
select 1, 1, 147  union all
select 2, 2, 148 union all
select 3, 3, 147 union all
select 4, 4, 149 union all
select 5, 4, 147;

Insert into product (id, name)
select 147, 'orange' union all
select 148, 'carrot' union all
select 149, 'Apple';

order_match.id = order_match_detail.order_match_idorder_match_detail.product_id = product.id

我想将order_status_id 不在 7 中的数据设为成功交易,从该成功交易开始,如果交易购买苹果,则苹果的列包含 1 否则如果不购买,则 0这是我的预期结果,我想将这些数据用于分析

 id (in order_match)    |    Orange  |  Carrot  |   Apple
    
    1                           1            0           0
    3                           1            0           0
    4                           1            0           1 

这个问题我可以用这个查询来解决

select om.id,
  count(DISTINCT case when omd.product_id = 147 THEN 1 END) Orange,
  count(DISTINCT case when omd.product_id = 148 THEN 1 END) Carrot,
  count(DISTINCT case when omd.product_id = 149 THEN 1 END) Apple
  from order_match om
left join order_match_detail omd
  on om.id = omd.order_match_id
where om.order_status_id in (4, 5, 6, 8)
group by om.id

真正的问题是,在我的真实数据库中,它包含 1550 product_id,如何使其自动生成,因此无需手动输入 product_id 直到 1550 product_id

这是小提琴https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=c0eb7fe1b012ab1c909d37e325a8d434

我已经尝试过这样的新查询,但仍然错误

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 product;

SET @sql = CONCAT('SELECT omd.order_match_id, ', @sql, ' from order_match_detail omd
left join order_match om
  on omd.order_match_id = om.id
left join product p
  on omd.product_id = p.id
where om.order_status_id in (4, 5, 6, 8)
group by omd.order_match_id');

PREPARE stmt FROM @sql;
EXECUTE stmt;

【问题讨论】:

  • 您打算有 1550 列吗?
  • 我们可以在存储过程中使用游标
  • 是的,先生,没问题,因为我想用另一个数据挖掘软件@tcadidot0 来分析它
  • 你能给我一个关于存储过程的建议吗@SivaKoteswaraRao
  • 所有那些product_id 都是从1 到1550 的编号?另外,您使用的是什么 MySQL/MariaDB 版本?

标签: mysql pivot


【解决方案1】:

您的查询几乎是正确的,您在别名而不是 p 上缺少您应该在第二个查询中使用“产品”,它会起作用

         SET group_concat_max_len = 1000000;
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 product;
    
    SET @sql = CONCAT('SELECT omd.order_match_id as id, ', @sql, ' from order_match_detail omd
    left join order_match om
      on omd.order_match_id = om.id
    left join product product
      on omd.product_id = product.id
    where om.order_status_id in (4, 5, 6, 8)
    group by omd.order_match_id');
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
             

如果您确定产品 ID 对每个 ID 都有唯一的名称,您可以从 GROUP_CONCAT(DISTINCT 中删除 DISTINCT

然后我会得到你在问题中提到的exact 结果,否则你会看到,Apple 列在前,Orange 列在后,总体结果为expected

【讨论】:

  • 感谢您的回答。不幸的是,当我想从 @sql 运行 PREPARE stmt 时,它显示错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'from order_match_detail omd left join order_match om on omd.order_match_id = o' 附近使用正确的语法
  • 我附上了 db fiddle 它的预期输出,dbfiddle.uk/…
  • 是的,先生,这就是我遇到的问题,在提出这个问题之前,小提琴是正确的,但是当它应用于我的真实数据库时,它总是像那个通知一样,我认为这个问题是因为一些产品名称的格式不正确,但我不知道
  • 您在语法上有错误,因为您在第一个查询别名中生成的列与产品表中的第二个查询不匹配,请尝试将我的解决方案完全复制粘贴到您的数据库中,您是否仍然看到错误 ?你能提供更多的示例数据吗,我认为你不应该在这里得到任何语法错误。
  • 好的,我认为这是包含一些特殊字符的产品名称 1. Bayam 1 ikat (± 330g) 2. Telur Asin Yes (1 pak = 6 butir) 3. Alpukat Mentega Medium 2 buah ( ± 280g/bh) 4. 促销:2 X Telur Ayam Kampung (isi 6)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 2020-10-13
  • 2010-10-28
相关资源
最近更新 更多