Good day. How to get the result of an SQL query based on data from another.

my $COUNTER =0; while ($COUNTER<=900){ my $sql = "SELECT f1 FROM TND where ID='$COUNTER'"; my $sth = $db->prepare($sql); $sth->execute(); my $A=$sth->fetchrow_arrayref(); my $sql1 = "select ID,f1,sum(f2) from TND where f1='@$A'"; my $sth1 = $db->prepare($sql1); my $B; my @B; while(@B=$sth1->fetchrow_arrayref()){ print "@B"; }; $COUNTER++; } 

The script is completed without giving me anything.

  • Interestingly, you get the ID in the second request. you in that line get the sum f2 of all records with f1 = $ A, and therefore in the ID field there will be from the first record according to this condition (not necessarily in order, but as lucky). In general, your query would be rewritten as select a.f1, sum(a.f2) from TND a, TND b where a.f1=b.f1 and b.id between 0 and 900 group by a.f1

1 answer 1

How to get the result of an SQL query based on data from another.

If the result of the first request is no longer needed for anything (and it seems like it is not needed), then create one request that immediately receives the necessary data.