mysql:12317
From: "KIYOSE" <"KIYOSE" <kiyose@xxxxxxxxxx>>
Date: Sat, 29 Oct 2005 03:07:39 +0900
Subject: [mysql 12317] 複合の集計?
KENJIです。
まだまだ初心者の私なのですが、
以下のような内容を行いたいと
思っています。
MySQLは4.1です。
以下のようなテーブルから
集計を行いたいのです。
<テーブル-1>
Aid date
1 2005-10-1
2 2005-10-2
3 2005-10-2
4 2005-10-3
5 2005-10-4
<テーブル-2>
id Aid g_id
1 1 2001
2 1 2002
3 2 2001
4 2 2005
5 3 2001
6 3 2005
7 4 2002
8 4 2001
9 4 2001
10 5 2001
テーブル-1のデータを
2005-10-1から2005-10-3までのAidを抽出して
テーブル-2のKEYにして
g_idを抽出して、g_idの集計を行いたいのです。
つまり、最終結果としては、
g_id 集計
2001 5
2002 2
2005 2
という結果を出したいのです。
自分なりには以下のようにやってみましたが、
一向にうまく行きません。
$bkday = "2005-10-1";
$today = "2005-10-3";
$sql = "select * from テーブル-1 where date>='$bkday' and
date<='$today'";
$result = $db_query->Db_Query_NVoid($db_name, $conn, $sql);
for($i=0;$i<mysql_num_rows($result); $i++)
{
$arr = mysql_fetch_array($result);
$sql = "select * fromテーブル-2 where Aid='$arr[Aid]' group by
g_id";
$res2 = $db_query->Db_Query_NVoid($db_name, $conn, $sql);
$arrsale = mysql_fetch_array($res2);
$item = $arrsale[g_id];
echo $item."<BR>";
}
何とか
正しい集計をお教えいただけませんでしょうか?
よろしくお願い申し上げます。
KENJI
-> 12317 2005-10-29 03:07 ["KIYOSE" <kiyose@xxx] 複合の集計? 12318 2005-10-29 03:32 ┣[nomoto <shin-1@xxxxx] 12319 2005-10-29 03:43 ┃┗[nomoto <shin-1@xxxxx] 12323 2005-10-29 19:16 ┗[柴垣 <akiro@xxxxxxxx]