how can I join 3 table with 2 table data sum - php

I have 3 table. purchase_order, goods_receive, purchase_transaction.
*** My purchase_order id is show as order_id.
1. I need sum all Quantity form goods_receive table as order id.
2. secondly I need sum amount form purchase_transaction tabel as order id.
see my attachment image for more clear >>
<?php
include("db.php");
$sqlnew="SELECT purchase_order.id , purchase_order.quantity,
purchase_order.unit_price, purchase_order.total_amount,
sum(goods_receive.quantity) as qua , sum(purchase_transaction.amount) as amount
FROM purchase_order
JOIN goods_receive ON purchase_order.id = goods_receive.order_id
JOIN purchase_transaction ON purchase_order.id = goods_receive.order_id
GROUP BY goods_receive.order_id";
$resultnew=mysql_query($sqlnew);
?>
<table class="table table-bordered data-table" border="1">
<thead>
<tr>
<th>Order ID</th>
<th>Quantity</th>
<th> Receive Quantity</th>
<th>Unit Price</th>
<th>Total Amount</th>
<th>Paid Amount</th>
</tr>
</thead>
<tbody>
<?php
while($rows=mysql_fetch_array($resultnew)){
?>
<tr class="gradeX">
<td><a href="purchase_order_single_details.php?id=<?php echo $rows['id']; ?>"><?php echo $rows['id']; ?></td>
<td><?php echo $rows['quantity']; ?></td>
<td><?php echo $rows['qua']; ?></td>
<td><?php echo $rows['unit_price']; ?></td>
<td><?php echo $rows['total_amount']; ?></td>
<td><?php echo $rows['amount']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!--Footer-part-->
Please help me...

Join with subqueries that calculate the totals you want
SELECT po.order_id, po.quantity, gr.quantity AS receive_quantity,
po.unit_price, po.total_amount, pt.amount AS paid_amount
FROM purchase_order AS po
JOIN (SELECT order_id, SUM(quantity) AS quantity
FROM good_receive
GROUP BY order_id) AS gr ON gr.order_id = po.order_id
JOIN (SELECT order_id, SUM(amount) AS amount
FROM purchase_transaction
GROUP BY order_id) AS pt ON pt.order_id = po.order_id

Related

How to hide repeating part of the retrieved data values using PHP , MYSQL & HTML

I have obtained following table as a result of this MySQL statement
select entry.id as id,
entry.payorder,
entry.bank,
entry.PFMS_SCHEME,
entry.name,
entry.vendor,
tally_head.head,
ledgers.amount,
entry.po,
entry.Description,
bank_details.Name as name,
entry.sum as sum
from entry join bank_details on entry.name=bank_details.id
join ledgers on ledgers.id=entry.id
join tally_head on ledgers.ledger=tally_head.id
group by tally_head.head
result i got
These are the mysql tables from which i am retrieving data
entrybank tableledgertally
the final output i want
the view i want
The HTML and PHP Code I used`
<table class="table">
<thead>
<tr>
<th>Voucher No </th>
<th>Payorder </th>
<th>Bank </th>
<th>PFMS_SCHEME </th>
<th>Party Name </th>
<th> For</th>
<th> Po/agmt/inv N.o </th>
<th>Ledger</th>
<th>Ledger Amount</th>
<th>Description/ Narration</th>
<th>sum</th>
</tr>
</thead>
<tbody>
<?php
$sql=mysqli_query($con,"select entry.id as cid,entry.payorder,entry.bank,entry.PFMS_SCHEME,entry.name,entry.vendor,tally_head.head,ledgers.amount,entry.po,entry.Description,bank_details.Name as name,entry.sum as sum from entry join bank_details on entry.name=bank_details.id join ledgers on ledgers.id=entry.id join tally_head on ledgers.ledger=tally_head.id group by tally_head.head ");
$cnt=1;
while($row=mysqli_fetch_array($sql))
?>
<tr>
<td><?php echo htmlentities($row['cid']);?></td>
<td><?php echo htmlentities($row['payorder']);?></td>
<td><?php echo htmlentities($row['bank']);?></td>
<td><?php echo htmlentities($row['PFMS_SCHEME']);?></td>
<td><?php echo htmlentities($row['name']);?></td>
<td><?php echo htmlentities($row['vendor']);?></td>
<td><?php echo htmlentities($row['po']);?></td>
<td><?php echo htmlentities($row['head']);?></td>
<td><?php echo htmlentities($row['amount']);?></td>
<td><?php echo htmlentities($row['Description']);?></td>
<td><?php echo htmlentities($row['sum']);?></td>
<td>
</td>
</tr>
<?php
$cnt++;
?>
</tbody>
</table>

How to select a name using foreign key, and present it on the table

I´m creating a school project and in this part I need to present the name of the person associated with this "n_processo" (id).
I have my tablets divided on the database like the printscreen, and I don´t know how to present the name of the person using the foreign key.
Turma Table:
I´m using a functions file, that have the function DBRead12 (that is selecting my "turma" table).
I have the user_especial table that have the names and the stranger key, like on this print.
User_Especial Table:
function DBRead12()
{
$sql="SELECT * FROM turma";
$result=DBExecute($sql);
while($res=mysqli_fetch_assoc($result))
{
$data[]=$res;
}
return $data;
}
<button class="collapsible">Consulta de Turmas</button>
<div class="content">
<br>
<div class="container" align="left">
<div class="well" align="left" style="width:70%">
<?php $admin = DBRead12()?>
<table id="example" class="table table-striped table-bordered" style="width:90%">
<thead>
<tr>
<th>Designação</th>
<th>Tipo</th>
<th>Diretor de Turma</th>
<th>Ano Letivo</th>
</tr>
</thead>
<tbody>
<?php foreach($admin as $cl)
{?>
<tr>
<td align=center><?php echo ($cl['designacao']) ?></td>
<td align=center><?php echo ($cl['tipo']) ?></td>
<td align=center><?php echo ($cl['diretor_turma']) ?></td>
<td align=center><?php echo ($cl['ano_letivo']) ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
In your query, you would use a join statement in order to get data from both tables at once
Example:
$sql="SELECT user_especial.nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
$sql="SELECT user_especial.nome as nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
This will give you all data from Turma Table and nome from User_Especial Table as nome.
$sql="SELECT user_especial.nome as nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
Also you must cut $cl['diretor_turma'] and add $cl['nome'].
So, your tbody will be:
<td align=center><?php echo ($cl['designacao']) ?></td>
<td align=center><?php echo ($cl['tipo']) ?></td>
<td align=center><?php echo ($cl['nome']) ?></td>
<td align=center><?php echo ($cl['ano_letivo']) ?>
</td>

Balance is not showing

Here is my code:
<table width="50%" border="2" bordercolor="green">
<tr>
<th>ItemName</th>
<th>Balance</th>
</tr>
<?php
if($qw="select DISTINCT(itemname) from details where DATE(date)<='$date' order by date desc"){
$qq = mysqli_query($con,$qw);
while($r=mysqli_fetch_array($qq,MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $r['itemname']; ?></td>
<td><?php echo $r['balance']; ?></td>
</tr>
<?php
}
}
?>
</table>
The given code is fetch product itemname but Balance is not fetch from the database.
Your query does not include the 'balance' field.
Change your query to include it:
select DISTINCT itemname, balance from details where ...

Inner Join tables and display in php

I want to select data from more tables with Inner join.
These are my tables.
teams (id, team_name)
badges (id, badgename, badgeimage, badgedescription)
teambadges (id, team_id, badge_id)
I want to write a statement that shows the team name with all the badges they have. I also want to display this in a table
This is my statement.
$sql = mysqli_query($connection, 'SELECT teams.team_name,badges.badgename
FROM teambadges
INNER JOIN teams ON teams.id = teambadges.team_id
INNER JOIN badges ON badges.id = teambadges.badge_id;');
Php:
<table class="table table-condensed table-striped table-bordered table-hover">
<thead>
<tr>
<th width="5%"><center>No</center></th>
<th>team id</th>
<th>badge id</th>
</tr>
</thead>
<tbody id="data">
<?php $no=1; while ($row = mysqli_fetch_array($sql)) { ?>
<tr>
<td align="center"><?php echo $no; ?></td>
<td><?php echo $row['team_name']; ?></td>
<td><?php echo $row['badgename']; ?></td>
</tr>
<?php $no++; } ?>
</tbody>
</table>
This is executed inside the php page but i keep getting this error : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
use
mysqli_query() or die(mysqli_error($connection))
to get your error
and check your query run successfully on mysql
i think you error is here
<td><?php echo $row['teams.name']; ?></td>
<td><?php echo $row['badges.name']; ?></td>
just chech if you have the right name column .
if you have there table with the same name
teams (id, team_name)
badges (id, badgename, badgeimage, badgedescription)
teambadges (id, team_id, badge_id)
you must do this :
<td><?php echo $row['team_name']; ?></td>
<td><?php echo $row['badgename']; ?></td>

Inner Join select only two records from mysql

I have a subject and a departments table, Each subject is associated with departments table. I am trying to select all subjects including departments name. The code bellow work perfect but show only two records. Any Help will be appreciated
//Select statement
$selects=$connection->query("SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
ON subjects.related_to = departments.dep_id");
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th>Sub Id</th>
<th>Subject Name</th>
<th>Related to Department</th>
<th>Related to Semester</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
while($result=$select->fetch_assoc()) {
?>
<tr>
<td><?php echo $result['id']; ?></td>
<td class="center"><?php echo $result['name']; ?></td>
<td class="center"><?php echo $result['dept']; ?></td>
<td class="center"><?php echo $result['related_to_sem']; ?></td>
<td class="center">
<a class="btn btn-info" href="#">
<i class="icon-edit icon-white"></i>
Edit
</a>
<a class="btn btn-danger" href="#">
<i class="icon-trash icon-white"></i>
Delete
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
select
subjects.id,subjects.name,
subjects.related_to,
subjects.related_to_sem,
departments.dept
from
subjects
LEFT OUTER JOIN departments ON subjects.id=departments.dep_id
Please try the following. I think you may have used the wrong fields in the join condition:
SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
/* ON subjects.id = departments.dep_id */
ON subjects.dep_id = departments.id
A "foreign key" in the subjects table for department is much more likely to be subjects.dep_id

Categories