0

我的数据库中有两张表,一张表包含旅行/用户相关信息:例如trip_id, username, leave_date, return_date,flight_estimateregistration_fee,第二张表是trip_id作为我的 FK 的费用表,以及所有费用表,例如hotelcost、bf、lunch for每个日期...因此每个日期trip_ip在此表中都有多个记录。

现在,我正在尝试使用 php 为用户创建一个包含总价的汇总表,当我使用以下代码时,它给出了一个日期正确的旅行记录,但如果它有多个日期,如果创建两个不同的记录..但我希望它作为一个记录(因为我正在计算那次特定旅行的总数)

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 回答