クエリtable_orderからのサンプルデータについて
//get unpaid orders
$sql = mysql_query("SELECT id,total,add_by FROM `order` WHERE status = '1'");
$res = mysql_num_rows($sql);
if($res>=1){
$tot = 0;
$ids = '';
while($row = mysql_fetch_array($sql)){
$ids .= $row['id'].",";
$tot += $row['total'];
$uid = $row['add_by'];
}
$ids = rtrim($ids, ",");
/*echo "<br>List of ID: ".$ids;
echo "<br>Total: ".$tot."<br>";
echo "<br>Add by: ".$uid."<br>";*/
//create bill
$sql_bill = "INSERT INTO `bill` (list,amount,user) VALUES('$ids','$tot','$uid')";
$query = mysql_query($sql_bill);
}
あなたの結果は
select group_concat(id) ,sum(total) ,add_by
from `order`
group by add_by
その後、あなたは、単一のクエリコマンドで
insert into table_bill (list , amount , user)
select group_concat(id) ,sum(total) ,add_by
from `order`
group by add_by
を行うことができますが、これはあなたの素晴らしいアイデアだと思う場合は、[ジャンクションテーブル](http://stackoverflow.com/a/32620163)をお読みください。 – Drew