[英]How to combine the values of two different tables with the same attributes
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. 我正在处理一个项目,其中包括显示carid(foreign key from tbl_vehicle) reg_num(registration number or plate number of a car, from tbl_vehicle)
和amount(from tbl_fuel)
,我成功显示了carid
, reg_num
和使用此SQL语句以HTML形式从表tbl_fuel
和tbl_vehicle
中tbl_fuel
amount
。
$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
. 但是我忘了还有一个名为tbl_maintenance的表,它具有与tbl_fuel相同的属性,即carid(foreign key from tbl_vehicle)
, amount
。 I need to display the values of this attributes from tbl_fuel
and tbl_maintenance
in single HTML form. 我需要以单个HTML形式显示tbl_fuel
和tbl_maintenance
中该属性的值。
this is my html form 这是我的HTML表格
<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? 请注意,上述代码可以正常运行,我的问题是,应在我当前的SQL语句中添加哪一行SQL行,以包含tbl_maintenance中的属性值并以我的HTML形式显示,并将tbl_maintenance和tbl_fuel的carid分组,并对tbl_maintenance和tbl_fuel?
Switch it around, make tbl_vehicle the main table, and do sub-queries for the sums: 切换一下,将tbl_vehicle设为主表,并对总和进行子查询:
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
SELECT tbl_fuel.carid,tbl_vehicle.reg_num,sum(trim(replace(tbl_fuel.amount, '$', '')) + 0.0) as FuelAmount , sum(trim(replace(tbl_maintenance.amount, '$', '')) + 0.0) as MaintenanceAmount
FROM tbl_fuel
LEFT JOIN tbl_vehicle
on tbl_fuel.carid=tbl_vehicle.carid
LEFT JOIN tbl_maintenance
on tbl_maintenance.carid=tbl_vehicle.carid
GROUP BY carid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.