使用php和mysql从两个不同的表+计算价格

时间:2013-07-31 14:50:27

标签: php mysql database

我的数据库中有两个表,一个表有旅行/用户相关信息:例如trip_idusernameleave_datereturn_date,{{1} },flight_estimate等,第二个表是费用表registration_fee作为我的FK,以及所有费用,例如hotelcost,bf,每个日期的午餐......所以每个trip_id在此表中有多个记录。

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

trip_ip

2 个答案:

答案 0 :(得分:1)

您可以使用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

答案 1 :(得分: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