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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘