当前位置:网站首页>PHP optimizes SQL queries in foreach
PHP optimizes SQL queries in foreach
2022-07-02 22:39:00 【If time comes again】
describe
The data to be queried needs to be associated 3 Tables , First use the first two tables left join After querying, use foreach Query the contents of the third table . But in foreach There is too much data in it , It will be very slow and sometimes collapse
An optimization method
stay foreach Find out all the data in the third table
Code :
# 1. Joint query of two tables
$list = Db::query("select a.id,a.organ_name,b.basctype,b.bascidno,b.bascname,b.bascaddress from tablea a left join tableb b on a.id=b.aid where a.createtime >= '2022-06-01' ");
if($list){
$new_table3_list = [];
$table3_list = Db::query("select aid,name,tel from tablec where name is null");
if($table3_list){
foreach($table3_list as $v){
$new_table3_list[$v['aid']] = [
'name'=>$v['name'],
'tel'=>$v['tel'],
];
}
}
foreach($list as $key=>$val){
if(!empty($new_table3_list[$val['id']])){
$val['name']=$new_table3_list[$val['id']]['name'];
$val['tel']=$new_table3_list[$val['id']]['tel'];
}
}
}
边栏推荐
- 数据库系统概论第一章简答题-期末考得怎么样?
- 100 important knowledge points that SQL must master: management transaction processing
- App page sharing password rails implementation
- Introduction to database system Chapter 1 short answer questions - how was the final exam?
- Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
- Unity3d learning notes 4 - create mesh advanced interface
- Radis:Linux上安装Redis(步骤)
- 服务器响应状态码
- Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
- From personal heroes to versatile developers, the era of programmer 3.0 is coming
猜你喜欢
UE4 game architecture learning notes
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Pointer and string
《Just because》阅读感受
任务和特权级保护
phpcms实现订单直接支付宝支付功能
Socket socket c/s end process
【外刊】睡眠与减肥
From personal heroes to versatile developers, the era of programmer 3.0 is coming
[shutter] shutter resource file use (import resource pictures | use image resources)
随机推荐
Market Research - current market situation and future development trend of genome editing mutation detection kit
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
Sql service intercepts string
Evolution of messaging and streaming systems under the native tide of open source cloud
phpcms实现订单直接支付宝支付功能
Oracle-游标
Pointer array parameter passing, pointer parameter passing
Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
Market Research - current market situation and future development trend of intravenous injection (IV) bottles
New feature of go1.18: trylock, which has been tossed n times
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
#include errors detected. Please update your includePath.
原生js添加样式的方法
对象与对象变量
Struct, bit segment, enumeration, union
LxC terminal login method
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Phpcms realizes the direct Alipay payment function of orders
【leetcode】1380. Lucky number in matrix