mysql - 行排名之间的关系

标签 mysql sql grouping joi

我有一个看起来像这样的表:

enter image description here

如果 imported 和 section 之间存在任何关系,则应将它们组合在一起。根据关系,它们应该被分组为 1,2,3,4,....

我尝试了一个看起来像这样的查询:

    select sec.section,sec.id, sec.imported, sec.id,
    case when imp.imported = sec.section or imp.imported is null then 1 ELSE 
    2  end  as rn
    from
  ( select section, id, imported from temp1) sec
    left outer join 
    (
    select imported, Section from temp1
    ) imp on imp.imported = sec.section

但在这种情况下,我的 rn 始终为 1。你能帮我查看一下这个查询吗? 我不确定如何解决这个问题。我们是否需要使用 while 循环来执行此操作,还是可以使用查询来完成?

示例创建脚本:

    create table temp1 (
    id int, imported int, section int, rn int, checked int default 0
    );
    insert into temp1(id, section, rn)           values (204, 718, 0);
    insert into temp1(id, imported, section, rn) values (997,718,034,0);
    insert into temp1(id, imported, section, rn) values (998,034,055,0);
    insert into temp1(id, imported, section, rn) values (111,453,234,0);
    insert into temp1(id, section, rn) values (908, 453,0);
    insert into temp1(id, imported, section, rn) values (231,234,890,0);
    insert into temp1(id, section, rn) values (342, 567,0);

我的最终结果应该是这样的:

enter image description here

我也尝试过使用 while 循环创建存储过程:

    DROP PROCEDURE IF EXISTS sp_recursiveimport;
    Delimiter $$
    CREATE PROCEDURE sp_recursiveimport()  -- (IN rnX integer)
    BEGIN
    DECLARE n INT DEFAULT 0;   DECLARE i,j,k INT DEFAULT 0;    SELECT COUNT(*) FROM temp1 INTO n;
    SET i=0; set @rn = 1; --  set @k = 0;
    WHILE i<n DO 
    set j = 0; select i;
      set @sec = (select ifnull(section,0) FROM temp1 LIMIT i,1);          
      set @imp = (select ifnull(imported,0) FROM temp1 LIMIT i,1); select @imp, @sec;
        update1: while j<n do   select j;
    --         if j=0 then
               if (select ifnull(imported,0) from temp1 limit j,1) = @sec and (select checked from temp1 limit j,1) = 0 then 
               set @update  = concat('update temp1 set rn = 1, checked = 1 where imported = ',@sec); select @update;    PREPARE stmt_name FROM @update;     EXECUTE Stmt_name;     DEALLOCATE prepare stmt_name;
               set @update1 = concat('update temp1 set rn = 1, checked = 1 where section = ',@sec); select @update1;   PREPARE stmt_name FROM @update1;     EXECUTE Stmt_name;     DEALLOCATE prepare stmt_name;
               set k = j;
               end if;
               if (select ifnull(section,0)          from temp1 limit j,1) = @imp and (select checked from temp1 limit j,1) = 0 then 
               set @update3 = concat('update temp1 set rn = 1, checked = 1 where section = ',@imp);  select @update3;    PREPARE stmt_name FROM @update3;     EXECUTE Stmt_name;     DEALLOCATE prepare stmt_name;
               set @update4 = concat('update temp1 set rn = 1, checked = 1 where imported = ',@imp); select @update4;    PREPARE stmt_name FROM @update4;     EXECUTE Stmt_name;     DEALLOCATE prepare stmt_name;
               set k = j;
               end if;

    --            set @sec = (select ifnull(imported,0) from temp1 limit k,1);
    --            set @imp = (select ifnull(section,0)          from temp1 limit k,1); select @sec, @imp;
        set j= j+1;
        end while update1;

      set i = i + 1;
    END WHILE;
    END;
    $$
    delimiter;

不确定为什么它不起作用。

最佳答案

这不是问题的答案,因为老实说,我不知道如何在 MySQL 5.x 中编写此查询。

无论如何,至少我想使用在 MySQL 8.0 或更高版本上可用的递归 CTE 来记录答案。在这里:

with recursive sect as (
  select id, imported, section, row_number() over() as rn
    from temp1 where imported is null
  union all
  select t.id, t.imported, t.section, s.rn
    from temp1 t
    join sect s on t.imported = s.section
)
select * from sect order by rn;

结果:

id   imported  section  rn
---  --------  -------  --
998  34        55       1
204  <null>    718      1
997  718       34       1
231  234       890      2
908  <null>    453      2
111  453       234      2
342  <null>    567      3

关于mysql - 行排名之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52560475/

相关文章:

mysql - 如何检查一个字符串是否包含列MySQL中的任何字符串

php - mysql全选,按orderid分组

php - 将多维数组中的重复数组键分组到子数组中

mysql - Ruby on Rails,我可以在加入另一个表时设置表别名吗?

mysql - sql 将行从一个表移动到另一个表

javascript - 如何使用 php 查询处理数据添加多字段输入

sql - 如何在 JPA/Hibernate 中执行原生 SQL 脚本?

where条件下的Mysql别名

mysql - Mysql 中的 Unix 时间日期

javascript - 在javascript中提取正则表达式组不起作用