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

标签 php mysql database

我的数据库中有两张表,一张表包含行程/用户相关信息:例如 trip_idusernameleave_datereturn_dateflight_estimateregistration_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/

相关文章:

php - 在查询中使用 "ORDER BY count"时出错

database - 订单行导入无法识别产品外部 ID?

php - 用符号替换数据库查询中的冗余日期(MySQL/PHP)

php - 如何正确地将 "$data"从一个 mySQL 查询输入到另一个查询中?

php - 如何将 HTML 添加到 WooCommerce 结帐时的 "billing_email"字段?

mysql - 如何使用 SQL 查询仅更新日期时间数据类型中的年份 (yyyy)?没有 dateadd 函数

mysql - 何时选择 Oracle 而不是 MySQL?

javascript - 将表字段名从 php 保存到 excel

mysql - 尝试保存小数时始终保存 999.999999

sql - mysql - 按一天中的小时搜索时间戳