douque9815 2019-03-05 05:29
浏览 280
已采纳

如何将具有相同属性的两个不同表的值组合在一起

I'm working with a project which includes to display the carid(foreign key from tbl_vehicle) reg_num(registration number or plate number of a car, from tbl_vehicle) and amount(from tbl_fuel), I successfully display the values of carid, reg_num and amount from the table tbl_fuel and tbl_vehicle in HTML form using this SQL statement.

$Withdraw = query("SELECT tbl_fuel.carid,tbl_vehicle.reg_num,sum(trim(replace(amount, '$', '')) + 0.0) as amount
                   FROM tbl_fuel
                    LEFT JOIN tbl_vehicle
                    on tbl_fuel.carid=tbl_vehicle.carid
                    GROUP BY carid");

but I forgot that there is another table named tbl_maintenance with the same attributes to tbl_fuel, which are carid(foreign key from tbl_vehicle), amount. I need to display the values of this attributes fromtbl_fuel and tbl_maintenancein single HTML form.

this is my html form

<div class="panel-body">
    <h3 align="center">Withdrawal Per Vehicle</h3>
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Plate Number</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($Withdraw as $w): ?>
                <?= '<tr>' ?>
                    <?= '<td>' . $w["carid"] . '</td>' ?>
                    <?= '<td>' . $w["reg_num"] . '</td>' ?>
                    <?= '<td>' . $w["amount"] . '</td>' ?>
                <?= '</tr>' ?>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

note that the codes stated above runs properly, my question is, what SQL line should be added to my current SQL statement to include attribute's value from tbl_maintenance and display it in my HTML form and group the carid of tbl_maintenance and tbl_fuel and sum the amount from tbl_maintenance and tbl_fuel?

  • 写回答

2条回答 默认 最新

  • douji8549 2019-03-05 05:42
    关注

    Switch it around, make tbl_vehicle the main table, and do sub-queries for the sums:

    SELECT v.carid,
           v.reg_num,
           IFNULL(f.sum_amount,0) + IFNULL(m.sum_amount,0) AS amount
    FROM tbl_vehicle v
    LEFT JOIN
      (SELECT carid,
              sum(trim(replace(amount, '$', ''))+0) sum_amount
       FROM tbl_fuel
       GROUP BY cardid) f ON f.carid = v.carid
    LEFT JOIN
      (SELECT carid,
              sum(trim(replace(amount, '$', ''))+0) sum_amount
       FROM tbl_maintenance
       GROUP BY cardid) m ON m.carid = v.carid
    WHERE f.sum_amount IS NOT NULL OR m.sum_amount IS NOT NULL
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划