gpt4 book ai didi

PHP SQL 多表分层 foreach 内的 foreach 内的 foreach 仅具有以上 1 级的引用

转载 作者:行者123 更新时间:2023-11-29 22:45:44 31 4
gpt4 key购买 nike

我需要在两种不同的模式下运行,因此“if”(第二个稍后构建一个大的 csv)

下面对于单个实例运行正常,但在第二个 (*) 的加载时间上失败,因为在前 7k 行中的每一行上运行。

我想避免可怕的事情

 set_time_limit(0);

我还在每个阶段挑选一些部分,因为这些也会在以后使用。

所以问题是实现下面的最快方法是什么?

可能是一个 SQL 语句并提取定义的值,如

table_c.tree , $row['apple'] 

表结构

表 A 有 7k 行,其中 $row['Code'] 可以重复 10 次

表 B 有 10k 行,其中 $row['Group'] 可以重复 5 次

表 c 有 2k 行,其中 $row['Group2'] 可以重复 5 次

 if (isset($_POST["go"])) { $where = 'WHERE xxxxx = 1';} else{$where = 'WHERE xxxxx = "'.$selected_val[1].'"';}

$stmt = $conn->prepare(" SELECT * FROM table_A $where "); $stmt->execute();

foreach ($stmt as $row) {
$Code = $row['Code'];

$stmt1 = $conn->prepare(" SELECT * FROM table_B WHERE Code = '$Code' "); $stmt1->execute();

foreach ( $stmt1 as $row1 ) {
if ($row['apple']) {$apple = $row['apple'];} // used to stop running rest all the time and also set $ for use later
else {$group1 = $row1['Group'];

$stmt2 = $conn->prepare(" SELECT * FROM table_c WHERE group2 = '$group1' "); $stmt2->execute();

foreach ( $stmt2 as $row2 ) {$group2 = $row2['group2'];}
$stmt3 = $conn->prepare(" SELECT * FROM table_d WHERE group3 = '$group2' "); $stmt3->execute();
foreach ( $stmt3 as $row3 ) {

$result [] = $row3['result']
}
}
}


/// do things with
$result
$Code
$group1
$row2['group2']
$row['apple']
}

第一个表格不是我的,所以我无法控制它们。

另一种方法可能是将一些新字段加载到“Table_a”中,但这只是将时间分配给其他函数。

关注@yoshiwaan

我在这里错过了速度技巧吗?如果我在开始时创建“my_result_chain”一次(因为所有数据都可以以良好的速度转储),那么只需调用 foreach 它就会运行得非常快。但由于有些表不包含上一级表的所有引用,因此它尽可能返回 null 而不是跟踪。这是通过使用 LEFT JOIN 来纠正的,但显然会创建多行,其中过滤键相同等,有没有办法选择哪个行/字段,DISTINCT 仅抓取顶层,并且每个表没有唯一的键,因为我需要从不同的表获取各种数据?对于引用 13 秒 v .03 秒(如果创建一次)。如果我向 foreach 的每一行添加一个关键数字,那是可行的,但不是一个好的解决方案。

最佳答案

简短提示:如果您不需要所有内容,请不要选择 *,只需选择您想要的列。

冗长的版本:不要在代码中执行此操作,而是将逻辑卸载到数据库中的 View 中。

这是一个给您的重要示例(您需要向下滚动才能看到全部内容):

create table table_A
(
xxxxxx int,
Code int
);

create table table_B
(
Code int,
`Group` varchar(20),
apple varchar(20)
);

create table table_C
(
`Group` varchar(20),
group2 varchar(20)
);

create table table_D
(
group3 varchar(20),
result varchar(20)
);

insert into table_A
values (1, 1);
insert into table_A
values (2, 2);
insert into table_A
values (3, 3);
insert into table_A
values (4, 4);

insert into table_B
values (1, 'snake', 'yes');
insert into table_B
values (1, 'lizard', '');
insert into table_B
values (2, 'canine', 'yes');
insert into table_B
values (2, 'feline', '');
insert into table_B
values (3, 'smallbird', 'yes');
insert into table_B
values (3, 'bigbird', '');
insert into table_B
values (4, 'bigfish', '');
insert into table_B
values (4, 'smallfish', '');


insert into table_C
values ('snake', 'python');
insert into table_C
values ('lizard', 'adder');
insert into table_C
values ('canine', 'corgi');
insert into table_C
values ('feline', 'lion');
insert into table_C
values ('ursine', 'grizzly');
insert into table_C
values ('smallbird', 'swallow');
insert into table_C
values ('bigbird', 'goose');
insert into table_C
values ('bigfish', 'baraccuda');
insert into table_C
values ('smallfish', 'minnow');
insert into table_C
values ('smallfish', 'herring');

insert into table_D
values ('python', 'big');
insert into table_D
values ('adder', 'scary');
insert into table_D
values ('asp', 'scary');
insert into table_D
values ('corgi', 'funny');
insert into table_D
values ('doberman', 'funny');
insert into table_D
values ('lion', 'evil');
insert into table_D
values ('tabby', 'evil');
insert into table_D
values ('swallow', 'spit');
insert into table_D
values ('goose', 'edible');
insert into table_D
values ('herring', 'red');
insert into table_D
values ('pike', 'weapon');

create view my_result_chain
as
select a.xxxxxx as filter, a.Code as Code, b.Group as Group1, c.group2 as Group2, d.result as Result
from
table_A a
join (table_B b)
on (b.Code=a.Code)
join (table_C c)
on (c.group=b.group)
join (table_D d)
on (d.group3=c.group2)
where
b.apple = '';

select * from my_result_chain;

select * from my_result_chain
where filter = 1;

drop view my_result_chain;
drop table table_A;
drop table table_B;
drop table table_C;
drop table table_D;

现在,在您的代码中,只需选择使用您想要的过滤器值,您就会在结果列中获得所有结果。

关于PHP SQL 多表分层 foreach 内的 foreach 内的 foreach 仅具有以上 1 级的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151425/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com