2011年4月20日水曜日

グループ分けして合計を表示-特定データを除いて表示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
 
 //データベース接続
 $link = mysql_connect('localhost', 'root', 'pass');
 if (!$link) {
     die('接続失敗です。'.mysql_error());
 }
 print('<p>接続に成功しました。</p>');
 
 // MySQLに対する処理
 print('<p>データを表示します。</p>');
 
 $sql = "select type, SUM(price) from `test`.`sampletable`  WHERE type != '本' GROUP BY type ORDER BY SUM( price ) DESC";
    
 $res = mysql_query($sql) or die(mysql_error());
 
 // Print out result
 while($row = mysql_fetch_array($res)){
  echo $row['type'] . "--" . $row['SUM(price)'] . "円";
  echo "<br />";
 }
 
 if (!$res ) {
      die('エラーが発生しました。'.mysql_error());
  }
 
 $close_flag = mysql_close($link);
 if ($close_flag){
     print('<p>切断に成功しました。</p>');
 }
 
?>

0 件のコメント:

コメントを投稿