Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 你的意思是???)我对存储过程很陌生,我的问题可能比你想象的更基本;-)我知道如何将项目插入子表。。我的意思是询问是否/如何可能将多个tid/id对传递给程序。我知道我可以用一个临时表来完成,但我更喜欢一个更简单的解决方案;t2,3;t1,2并将其拆分为令_Sql_Mysql_Database_Optimization_Recursion - Fatal编程技术网

Sql 你的意思是???)我对存储过程很陌生,我的问题可能比你想象的更基本;-)我知道如何将项目插入子表。。我的意思是询问是否/如何可能将多个tid/id对传递给程序。我知道我可以用一个临时表来完成,但我更喜欢一个更简单的解决方案;t2,3;t1,2并将其拆分为令

Sql 你的意思是???)我对存储过程很陌生,我的问题可能比你想象的更基本;-)我知道如何将项目插入子表。。我的意思是询问是否/如何可能将多个tid/id对传递给程序。我知道我可以用一个临时表来完成,但我更喜欢一个更简单的解决方案;t2,3;t1,2并将其拆分为令,sql,mysql,database,optimization,recursion,Sql,Mysql,Database,Optimization,Recursion,你的意思是???)我对存储过程很陌生,我的问题可能比你想象的更基本;-)我知道如何将项目插入子表。。我的意思是询问是否/如何可能将多个tid/id对传递给程序。我知道我可以用一个临时表来完成,但我更喜欢一个更简单的解决方案;t2,3;t1,2并将其拆分为令牌,然后将每个令牌插入深度为0的children表中(我知道这很糟糕,但MySQL还有很长的路要走)@f00你能告诉我具体怎么做吗?看起来MySQL甚至没有拆分字符串的功能。。令人毛骨悚然:-/ Table t1: id val


你的意思是???)我对存储过程很陌生,我的问题可能比你想象的更基本;-)我知道如何将项目插入子表。。我的意思是询问是否/如何可能将多个tid/id对传递给程序。我知道我可以用一个临时表来完成,但我更喜欢一个更简单的解决方案;t2,3;t1,2并将其拆分为令牌,然后将每个令牌插入深度为0的children表中(我知道这很糟糕,但MySQL还有很长的路要走)@f00你能告诉我具体怎么做吗?看起来MySQL甚至没有拆分字符串的功能。。令人毛骨悚然:-/
Table t1:
    id
    valA
    valB

Table t2:
    id
    valA
    valB
Example data:

t1 (id, valA, valB):
    1, a, B
    2, b, J
    3, d, E
    4, d, B
    5, c, G
    6, h, J

t2 (id, valA, valB):
    1, b, E
    2, d, H
    3, g, B


Example 1:

Input: Row 1 in t1
Output: 
    t1/4, t2/3
    t1/3, t2/2
    t2/1
    ...


Example 2:

Input: Row 6 in t1
Output:
    t1/2
    t2/1
create view t12 (tbl, id, vala, valb) as (
  (select 't1', id, vala, valb from t1)
  union
  (select 't2', id, vala, valb from t2)
)
with recursive descendants (tbl, id, vala, valb) as (
  (select *
  from t12
  where tbl = 't1' and id = 1) -- the query that identifies the seed rows, here just t1/1
  union
  (select c.*
  from descendants p, t12 c
  where (p.vala = c.vala or p.valb = c.valb)) -- the recursive term
)
select * from descendants;
drop table if exists t1;
create table t1
(
id int unsigned not null auto_increment primary key,
valA char(1) not null,
valB char(1) not null
)
engine=innodb;

drop table if exists t2;
create table t2
(
id int unsigned not null auto_increment primary key,
valA char(1) not null,
valB char(1) not null
)
engine=innodb;

drop view if exists t12;
create view t12 as
select 1 as tid, id, valA, valB from t1
union
select 2 as tid, id, valA, valB from t2;

insert into t1 (valA, valB) values 
('a','B'),
('b','J'),
('d','E'),
('d','B'),
('c','G'),
('h','J');

insert into t2 (valA, valB) values 
('b','E'),
('d','H'),
('g','B');

drop procedure if exists find_children;

delimiter #

create procedure find_children
(
in p_tid tinyint unsigned,
in p_id int unsigned 
)
proc_main:begin

declare done tinyint unsigned default 0;
declare dpth smallint unsigned default 0;


create temporary table children(
 tid tinyint unsigned not null,
 id int unsigned not null,
 valA char(1) not null,
 valB char(1) not null,
 depth smallint unsigned default 0,
 primary key (tid, id, valA, valB)
)engine = memory;

insert into children select p_tid, t.id, t.valA, t.valB, dpth from t12 t where t.tid = p_tid and t.id = p_id; 

create temporary table tmp engine=memory select * from children;

/* http://dec.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

while done <> 1 do

    if exists(
    select 1 from t12 t 
      inner join tmp on tmp.valA = t.valA or tmp.valB = t.valB and tmp.depth = dpth) then

        insert ignore into children
        select 
        t.tid, t.id, t.valA, t.valB, dpth+1 
      from t12 t
      inner join tmp on tmp.valA = t.valA or tmp.valB = t.valB and tmp.depth = dpth;

        set dpth = dpth + 1;            

        truncate table tmp;
        insert into tmp select * from children where depth = dpth;

    else
        set done = 1;
    end if;

end while;

select * from children order by depth;

drop temporary table if exists children;
drop temporary table if exists tmp;

end proc_main #


delimiter ;


call find_children(1,1);

call find_children(1,6);