我的数据库中有两张表,一张表包含行程/用户相关信息:例如 trip_id
、username
、leave_date
、return_date
、flight_estimate
、registration_fee
等,第二个表是费用表 trip_id
作为我的 FK,以及所有每个日期的费用,例如酒店费用、男朋友、午餐...因此每个 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;
}
最佳答案
您可以使用MySQL的GRUOP BY
:
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
关于php - 从两个不同的表计算价格+使用php和mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17973724/