0

データベースに 2 つのテーブルがあり、1 つのテーブルには旅行/ユーザー関連の情報が含まれていますtrip_id: 、、、、、などusername。各日付...それぞれがこのテーブルに複数のレコードを持っています。leave_datereturn_dateflight_estimateregistration_feetrip_idtrip_ip

今、私はユーザーのためにphpを使用して合計価格の要約テーブルを作成しようとしています.記録..しかし、私はそれを1つの記録として欲しい(その特定の旅行の総計を計算しているので)

if(isset($user))
{
    $sql = 
          " SELECT Trip.trip_id as new_tripid"
        . "      , destination"
        . "      , leave_date"
        . "      , return_date"
        . "      , date(last_updatedate) as updateddate"
        . "      , flight_estimate"
        . "      , registration_fee"
        . "      , hotel_cost"
        . "      , breakfast_cost"
        . "      , lunch_cost"
        . "      , dinner_cost"
        . "      , parking_cost"
        . "      , taxi_cost"
        . "      , rental_car_cost"
        . "      , mileage"
        . "      , fuel_cost"
        . "      , other_cost"
        . "   FROM Trip"
        . "      , Expense"
        . "  WHERE Trip.username='" . $user ."'"
        . "    AND Trip.trip_id = Expense.trip_id"
    ;
    $result = mysql_query($sql);

    $num_rows = mysql_num_rows($result);

    $o = '<table id="myTable1" border="1" style="border:1px solid orange; background-color: #f3f3f3;"><thead><tr><th>Trip ID</th><th>Destination</th><th>Leave Date</th><th>Return Date</th><th>Total</th><th>Submit Date</th><th>Status</th></tr></thead><tbody>';

} else {
    echo('not valid user');
}

if (!$result) {
   die(mysql_error());
}

if ($num_rows<=0){
    echo('not valid user');
}
elseif ($num_rows>0)
{

    while($row = mysql_fetch_array($result)) {
        $grandtotoal = $row['flight_estimate'] + $row['registration_fee'] + 
                       $row['hotel_cost']+$row['breakfast_cost']+$row['lunch_cost']+
                       $row['dinner_cost']+$row['parking_cost']+$row['taxi_cost']+
                       $row['rental_car_cost'] +$row['mileage']+$row['fuel_cost']+$row['other_cost'];


        $o .= '<tr><td align = "center" style="width:absolute; height:30px;">'.
               $row['new_tripid'].'</td><td align = "center" style="width:absolute;height:30px;">'.
               $row['destination'].
               '</td><td align = "center" style="width:absolute height:30px;">'.
                $row['leave_date'].
                '</td><td align = "center" style="width:absolute; height:30px;">'.$row['return_date'].
                '</td><td align = "center" style="width:absolute; height:30px;">'.
                $grandtotoal.'</td><td align = "center" style="width:absolute; height:30px;">'.
                $row['updateddate'].'</td></tr>';
    }
    $o .= '</tbody></table>';

    echo $o;
}
4

2 に答える 2

1

GRUOP BYMySQLを使用できます。

SELECT Trip.trip_id as new_tripid,destination, leave_date, return_date,
    date(last_updatedate) as updateddate, flight_estimate , registration_fee,
    hotel_cost , breakfast_cost, lunch_cost,dinner_cost, parking_cost,taxi_cost,
    rental_car_cost, mileage , fuel_cost, other_cost
FROM Trip, Expense
WHERE Trip.username='$user' AND Trip.trip_id = Expense.trip_id
GROUP BY Trip.username
于 2013-07-31T14:54:40.913 に答える
0
SELECT Trip.trip_id as new_tripid,destination, leave_date, return_date,
    date(last_updatedate) as updateddate, flight_estimate , registration_fee,
    SUM(hotel_cost, breakfast_cost, lunch_cost, dinner_cost, parking_cost, taxi_cost, 
    rental_car_cost, mileage, fuel_cost, other_cost) as total_cost
FROM Trip, Expense
WHERE Trip.username='$user' AND Trip.trip_id = Expense.trip_id
GROUP BY Trip.username
于 2013-07-31T14:55:23.080 に答える