<?php
include '../mysql.php';
// N+1问题
// customer_rec表 customer_id 关联 customer表
$rec = $DB->query("select * from `customer_rec`");
$rec_id = array();
$cid = array();
$rec_data = array();
while ($recs = $DB->fetch_assoc($rec)){
if (!in_array($recs['customer_id'],$rec_id)){
$rec_id[] = $recs['customer_id'];
$rec_data[$recs['customer_id']] = $recs;
}
}
$c = $DB->query("select * from `customer` where id in (".implode(',',$rec_id).")");
while ($rec = $DB->fetch_assoc($c)){
$cid[$rec['id']] = $rec;
}
foreach ($rec_data as $data){
// $rec_data[$data['customer_id']]['client_name'] = $cid[$data['customer_id']];
$rec_data[$data['customer_id']]['client_name'] = $cid[$data['customer_id']]['client_name'];
}
print_r($rec_data);
|