我在同一个数据库中有两个mysql表。第一个表是' coi_system'第二,' monthly_saving' id是从第一个表引用的。我想将这两个表格数据显示在this table。
所以,' monthly_saving'应显示在Cost Saving Monthly html列中,引用“coi_system”的ID。但事实证明它显示了1月17日的所有数据。列中的每个结果使每个结果显示为like this。这是代码,
<?php
$result = mysqli_query($conn,"SELECT * FROM coi_system, monthly_saving where monthly_saving.id=coi_system.id");
while ($row = mysqli_fetch_assoc($result)) {
$accountcode=$row['accountcode'];
$department=$row['department'];
$person_in_charge=$row['person_in_charge'];
$project_title=$row['project_title'];
$objective=$row['objective'];
$how_to_do=$row['how_to_do'];
$activities=$row['activities'];
$project_started=$row['project_started'];
$project_completed=$row['project_completed'];
$target_cost_saving=$row['target_cost_saving'];
$costsaving_afterjustification=$row['costsaving_afterjustification'];
$costsaving_monthly=$row['costsaving_monthly'];
$sum_of_month=$row['sum_of_month'];
$targetsaving_currentmonth=$row['targetsaving_currentmonth'];
$avg_monthly_saving=$row['avg_monthly_saving'];
$todays_date=$row['todays_date'];
$current_month=$row['current_month'];
$Jan=$row['Jan'];
$Feb=$row['Feb'];
$Mac=$row['Mac'];
$Apr=$row['Apr'];
$May=$row['May'];
$Jun=$row['Jun'];
$Jul=$row['Jul'];
$Aug=$row['Aug'];
$Sep=$row['Sep'];
$Oct=$row['Oct'];
$Nov=$row['Nov'];
$Dis=$row['Dis'];
$id=$row['id'];
?>
<div class="scroll">
<tr id="row1">
<td> </td>
<td id="accountcode_row1"><div> <?php echo $accountcode;?></div></td>
<td id="department_row1"><div> <?php echo $department;?></div></td>
<td id="pic_row1"><div> <?php echo $person_in_charge;?></div></td>
<td id="protitle_row1"><div> <?php echo $project_title;?></div></td>
<td id="objective_row1"><div> <?php echo $objective;?></div></td>
<td id="howtodo_row1"><div><?php echo $how_to_do;?></div></td>
<td id="activities_row1"><div> <?php echo $activities;?></div></td>
<td id="prostart_row1"><div> <?php echo $project_started;?></div></td>
<td id="procompl_row1"><div> <?php echo $project_completed;?></div></td>
<td id="targetcost_row1"><div> <?php echo $target_cost_saving;?></div></td>
<td id="costafter_row1"><div> <?php echo $costsaving_afterjustification;?></div></td>
<td><div><?php echo $Jan;?></div></td>
<td><div><?php echo $Feb;?></div></td>
<td><div><?php echo $Mac;?></div></td>
<td><div><?php echo $Apr;?></div></td>
<td><div><?php echo $May;?></div></td>
<td><div><?php echo $Jun;?></div></td>
<td><div><?php echo $Jul;?></div></td>
<td><div><?php echo $Aug;?></div></td>
<td><div><?php echo $Sep;?></div></td>
<td><div><?php echo $Oct;?></div></td>
<td><div><?php echo $Nov;?></div></td>
<td><div><?php echo $Dis;?></div></td>
<td><div><?php echo $sum_of_month; ?></div></td>
<td><div><?php echo $avg_monthly_saving; ?></div></td>
<td><div><?php echo $todays_date; ?></div></td>
<td><div><?php echo $current_month; ?></div></td>
<td><div></div></td>
<td><div> <?php echo $targetsaving_currentmonth;?></div></td>
&#13;
我不确定错误在哪里,但我尝试过这样写,$result = mysqli_query($conn,"SELECT * FROM coi_system, monthly_saving where id");
但这不是解决方案。我真的需要有人帮助,因为我对mysqli并不擅长。谢谢你提前!
更新
这是来自&#39; coi_system&#39;的表monthly_saving和this。 id被引用的地方。已解决但新问题,目前只有2行结果出现,其中包含了每月一次的&#39;数据。其余的没有出现,因为他们没有任何月度节省&#39;数据。那么如何展示它们呢?
答案 0 :(得分:0)
试试这个
SELECT * from coi_system, monthly_saving where monthly_saving.id=coi_system.id;
答案 1 :(得分:0)
SELECT * FROM coi_system INNER JOIN monthly_saving ON 'monthly_saving.id' = 'coi_system.id'
答案 2 :(得分:0)
SELECT coi_system.*, monthly_saving.* FROM coi_system RIGHT JOIN monthly_saving ON monthly_saving.id=coi_system.id;
如果每月保存为空,则右连接可以解决您的问题。