Sum and join 2 tables - php

I want to sum and join 2 tables. I am getting a wrong answer. What am i doing wrong please
$stmt = $pdo->query("
SELECT
a.prod_name, a.prod_size,
b.prod_name, b.prod_size,
SUM(b.qty_bought) qty_bot,
SUM(a.prod_qty) qty_received
FROM tbl_distribution a
JOIN tbl_sales_bar b
ON a.staff_id = b.staff_id
WHERE a.staff_id = '$_GET[id]'
GROUP BY b.prod_id
");
WHILE($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo '<tr>
<td>'.$row["prod_name"].'</td>
<td>'.$row["prod_size"].'</td>
<td>'.$row["qty_received"].'</td>
<td>'.$row["qty_bot"].'</td>
<td></td>
</tr>';
}
what i want is to get the sum of quantity bought and the sum of product quantity so that when i minus the product quantity for the quantity bought, i will get the balance which is the difference between the product quantity - quantity bought = available quantity
Thanks
Edit:
let me use images to show what i intend to have as result:
thanks

Related

How to add column if 2 other field is same [duplicate]

Building an inventory system. I have lots of products and each product has three different variables. So for stock totals I want to group by the two columns (product & size) and sum quantity to get stock total.
product
Size
Quantity
Widget one
2
275
Widget one
2
100
Widget two
3
150
Widget two
2
150
What I want for output:
product
Size
Quantity
Widget one
2
375
Widget two
3
150
Widget two
2
150
I figured out how to group by one column and sum using the code below:
$query = "SELECT product, SUM(Quantity) FROM inventory GROUP BY product";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['product']. " = ". $row['SUM(Quantity)'];
echo "<br />";
}
?>
I am just stuck on grouping by both columns. Is it possible? or should I just create three different products for the of the three sizes and eliminate that column? Thanks.
Based on your example table, it appears you want to be grouping on product rather than id. You merely need to add the Size column to both the SELECT list and the GROUP BY
$query = "SELECT
product,
Size,
SUM(Quantity) AS TotalQuantity
FROM inventory
GROUP BY product, Size";
Note that I have added a column alias TotalQuantity, which will allow you to more easily retrieve the column from the fetched row via the more sensible $row['TotalQuantity'], rather than $row['SUM(Quantity)']

PHP counting records from other table using id

I have a table prices that looks like this:
id - price - amount
I have an other table winners that keeps what price the users have won. It looks like this:
id - userid - priceid
So the id of my table prices is the sale as the priceid in the winners table.
Now let's say I want the amount of winners that have won price X and price X has id 1 for example. I've tried writing a query but it doesn't seem to make much sense.
$price = "X";
$count = $conn->fetchAll("SELECT winners.id FROM winners INNER JOIN prices ON winners.priceid=prices.id WHERE prices.price='".$price."'");
If anyone can help me out I would appreciate it very much
Many thanks in advance!
If you want the number that have won price X :
$count = $conn->fetchAll("SELECT COUNT(winners.id) AS nb_winners FROM winners INNER JOIN prices ON winners.priceid=prices.id WHERE prices.price='".$price."'");

How can I make a best-sellers list?

I want to make a "best sellers" list using information from previous orders. What I currently have is something like this;
Product Quantity
2227 30
1722 3
1851 7
2227 10
1722 4
1863 1
etc....
The first column (product) is the unique ID in the database for each product. The quantity is of course how many items have been sold. Each row is for one order. So the ID 2227 appears two times on this list.
How can I sort this data so that I get a total of how many times ID 2227 is sold?
My PHP at the moment is:
$SQL_best = "SELECT c.company, co.id, cod.productId, cod.quantity
FROM customers c
LEFT JOIN customers_orders co ON c.id = co.custId
LEFT JOIN customers_orders_details cod ON co.id = cod.orderId
WHERE c.reseller =1
AND c.status != 99
AND c.id = ".$intCustomerId;
$result_best = $objDB->sqlExecute($SQL_best);
*some html code here*
<table style="margin:0px auto;">
<tr>
<th>Product</th>
<th>Quantity</th>
</tr>
<?php
while($obj_best = $objDB->getObject($result_best)) {
if ($obj_best->quantity > 0) { // don't include negatve quantaties (RMA's / refunds)
echo "<tr>";
echo "<td>".$obj_best->productId."</td>";
echo "<td>".$obj_best->quantity."</td>";
echo "</tr>";
}
}
?>
</table>
MySQL query
So I need to add together all the $obj_best->productId´s. How do I do that in this case? Or should I be editing my query?
This is an idea, you change the SQL string like this:
$SQL_best = "SELECT c.company, co.id, cod.productId,
SUM(cod.quantity) AS quantity
FROM customers c
LEFT JOIN customers_orders co ON c.id = co.custId
LEFT JOIN customers_orders_details cod ON co.id = cod.orderId
WHERE c.reseller = 1
AND c.status != 99
AND c.id = " . $intCustomerId . "
GROUP BY cod.productId
HAVING quantity > 0
ORDER BY quantity DESC";

How to get grand total of many tables' totals

I have 500 tables, all containing records of dollar amounts in the field "sales".
The following gets me the total sales of green items per table:
$tabletotal = mysql_query("SELECT sum(sales) FROM databasename.$tablename
WHERE type = 'green' AND descr LIKE '%SOLD%' ");
$row = mysql_fetch_assoc($tabletotal);
$answer= $row['sum(sales)'];
So if a particular table has 4 green items and they were sold for $4, $6, $7, and $3, then the answer I get above is $20.
Now, what I don't know how to do, is to get the total of all green items in ALL 500 tables. So if table 1 has a total of $20, table 2 a total of $36, table 3 a total of $15, and so on, I want to get the grand total in all tables together (in this case $71 but it would be more for 500 tables obviously).
Any help anyone?
Probably best to use a for loop:
$tables = array('tbl1', 'tbl2', 'tbl3', ..., 'tbl499');
$total = 0;
foreach ($tables as $t)
{
$tabletotal = mysql_query("SELECT sum(sales) tot FROM databasename.$t WHERE type = 'green' AND descr LIKE '%SOLD%'");
$row = mysql_fetch_assoc($tabletotal);
$total += $row['tot'];
}
Alternatively, you could generate a giant SQL query using UNIONs, but it's essentially the same thing.
As Dream Eater mentions; you shouldn't be in this situation in the first place.

MySQL: Group by two columns and sum

Building an inventory system. I have lots of products and each product has three different variables. So for stock totals I want to group by the two columns (product & size) and sum quantity to get stock total.
product
Size
Quantity
Widget one
2
275
Widget one
2
100
Widget two
3
150
Widget two
2
150
What I want for output:
product
Size
Quantity
Widget one
2
375
Widget two
3
150
Widget two
2
150
I figured out how to group by one column and sum using the code below:
$query = "SELECT product, SUM(Quantity) FROM inventory GROUP BY product";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['product']. " = ". $row['SUM(Quantity)'];
echo "<br />";
}
?>
I am just stuck on grouping by both columns. Is it possible? or should I just create three different products for the of the three sizes and eliminate that column? Thanks.
Based on your example table, it appears you want to be grouping on product rather than id. You merely need to add the Size column to both the SELECT list and the GROUP BY
$query = "SELECT
product,
Size,
SUM(Quantity) AS TotalQuantity
FROM inventory
GROUP BY product, Size";
Note that I have added a column alias TotalQuantity, which will allow you to more easily retrieve the column from the fetched row via the more sensible $row['TotalQuantity'], rather than $row['SUM(Quantity)']

Categories