subtract from two different table - php

i have 3 tables tables "productlist, sales, return" so let say for example i have 3 sales and 2 return as given below.
this is the ff data from productlist
id | pcode | pname | pdesc |
1 | 222 | 33uf | 10v |
this is the ff data from sales
id | pcode | total | profit
1 | 222 | 200 | 10
2 | 222 | 100 | 10
3 | 222 | 200 | 10
this is the ff data from return
id | pcode | total | lose
3 | 222 | 200 | 10
4 | 222 | 100 | 10
My problem is this. I want to select data from productlist and sum the "total" and "profit" Value from sales and sum the "total" and "lose" value from return. And then subtracting my two table to get the result. The expected result must be something like this.
id | pcode | pname | pdesc | total | profit |
1 | 222 | 33uf | 10v | 200 | 10 |
I have this ff code but I can't subtract "total" from sales to "total" from return and "profit" from sales and "lose" from return.
$result = mysql_query("SELECT
productlist.*,
SUM(sales.total)-SUM(return.total) as total,
SUM(sales.profit)-SUM(return.lose) as profit
FROM productlist
LEFT JOIN sales ON sales.pcode = productlist.pcode AND return ON return.pcode = productlist.pcode
GROUP BY pcode
ORDER BY total ASC");

You seem to be trying to join two tables with AND, that's not quite right ;)
Try this:
...
LEFT JOIN `sales` USING (`pcode`)
LEFT JOIN `return` USING (`pcode`)
...
I'm not completely certain this'll work, it may complain of column `pcode` is ambiguous. If this happens, try this instead:
...
LEFT JOIN `sales` ON `sales`.`pcode` = `productlist`.`pcode`
LEFT JOIN `return` ON `return`.`pcode` = `productlist`.`pcode`
...

The structure of your query is not going to return the right results. Not matter how you fix the syntax, you will still be getting a cartesian product between the sales and returns for a given product.
One fix is to do aggregations before the joins:
SELECT pl.*,
(coalesce(s.total, 0) - coalesce(r.total, 0)) as total,
(coalesce(s.profit, 0) - coalesce(r.lose, 0)) as profit
FROM productlist pl left join
(select pcode, sum(total) as total, sum(profit) as profit
from sales
group by pcode
)
on s.pcode = pl.pcode left join
(select pcode, sum(total) as total
from return
group by pcode
) r
on r.pcode = pl.pcode
ORDER BY total ASC;

Related

Why insert data is not shown, only update data is show?

I have three table.First table shows input data, second table shows output data and third is total table where we shows our product total balance.
1.input table
Date | product_name | in_qty
5/7/19 | A | 10
5/7/19 | B | 15
6/7/19 | A | 10
6/7/19 | C | 20
2.output table
Date | product_name | out_qty
7/7/19 | A | 10
8/7/19 | B | 10
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
There my problem is in input table product C is not show in the total table. Which product add in the output table only those product total shown in the total table.
$res=mysqli_query($con, "SELECT i.product_name, i.in_qty, o.product_name,o.out_qty
FROM input i, output o
WHERE i.product_name= o.product_name");
while($row=mysqli_fetch_array($res))
{
$in_name = $row['product_name'];
$inqty = $row['Input_qty'];;
$out_name = $row['product_name'];
$outqty = $row['Out_qty'];
$sql=mysqli_query($con, "INSERT INTO total (product_name,Input_qty,Out_qty)
VALUES('$in_name','$inqty','$outqty')");
$sql2=mysqli_query($con, "UPDATE total t2
INNER JOIN (
SELECT product_name, SUM(in_qty) as qty_total
FROM input
GROUP BY product_name
) t1 ON t2.product_name= t1.product_name
SET t2.in_qty = t1.qty_total");
}
My expectation is :
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
C | 20 | | 20
your mysql query is wrong.
use following query .
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
in your case:
SELECT input.product_name , input.in_qty , output.out_qty
FROM input
LEFT [OUTER] JOIN output
ON input.column = output.column;

SQL Complexed Join

I want to list a Purchase Record for a customer by the following tables.
Table: Invoice
----------------------
invID | cusID | total
----------------------
1 | 1 | 10.5
Table: Invoice Item
--------------------
invID | prodID
---------------
1 | 1
1 | 3
Now I want to output as one row Like this: (or in PHP Fetched Row Table)
invID | cusID | prodID | total
-------------------------------
1 | 1 | 1, 3 | 10.5
What have I tried:
SELECT i.*, ii.prodID FROM invoice i, invoiceitem ii WHERE cusID = '1' AND i.invID = ii.invID
Result:
invID | cusID | prodID | total
-------------------------------
1 | 1 | 1 | 10.5
1 | 1 | 3 | 10.5
I think this will work for you. Haven't tested it so there might be a minor typo somewhere, but the concept should work.
SELECT i.invID, i.cusID, GROUP_CONCAT(ii.prodID) `prodID`, i.total
FROM invoice i
INNER JOIN invoiceitem ii ON i.invID = ii.invID
GROUP BY i.invID
You are just missing a GROUP_CONCAT over the prodIDs. You should also use ANSI INNER JOIN syntax, in preference to joining in the WHERE clause. Although MySql doesn't complain, it is also good practice to include all non-aggregated select fields in the GROUP BY, for compliance with other RDBMS's
SELECT i.invID, i.cusID, GROUP_CONCAT(ii.prodID) as prodID, i.total
FROM invoice i INNER JOIN invoiceitem ii ON i.invID = ii.invID
WHERE cusID = '1'
GROUP BY i.invID, i.cusID, i.total;

Double SQL Query - JOIN

I need a double SELECT sql query from 2 different tables with names visits & items
1.: SELECT visitid, visitdate, visitreason FROM visits WHERE personid = 10
2.: SELECT itemid, itemname, itemtime FROM items WHERE itemvisitid= visitid
I think I need to do a JOIN but don’t know exactly how.
Table examples:
Table: visits
visitid | personid | visitdate | visitreason
1 | 10 | 05/07/2014 | no reason
2 | 10 | 06/07/2014 | some reason
3 | 12 | 06/07/2014 | no reason
4 | 10 | 12/07/2014 | some other reason
Table: items
itemid | personid | itemvisitid | itemname | itemtime
1 | 10 | 2 | box | 23
2 | 10 | 2 | clock | 70
3 | 10 | null | water | 50
4 | 10 | null | paper | 40
5 | 12 | 3 | box | 26
What I have now is this:
$query = "SELECT visitid, visitdate, visitreason FROM visits WHERE personid = '10' ORDER BY visitdate DESC";
// 2nd select: "SELECT itemid, itemname, itemtime FROM items WHERE itemvisitid= visitid";
$db->setQuery($query);
$results = $db->query();
while($row = mysqli_fetch_array($results)){
echo "<tr>
<td>".$row['visitid'].", ".$row['visitdate']."</td>
<td>".$row['visitreason']."</td>
<td>".$row['itemid'].",".$row['itemname'].", ".$row['itemtime']."</td>
</tr>";
}
I need results to be something like this:
<tr>
<td>1, 05/07/2014</td><td>no reason</td><td></td>
<td>2, 06/07/2014</td><td>some reason</td><td>1, box, 23<br />2, clock, 70</td>
<td>4, 12/07/2014</td><td>some other reason</td><td></td>
</tr>
I guess your might to use GROUP_CONCAT like this:
DEMO: http://sqlfiddle.com/#!2/9d4e22/15
SELECT visitid, DATE_FORMAT(visitdate,'%m/%d/%Y'), visitreason,
GROUP_CONCAT(itemid,itemname, itemtime)
FROM visits left join items on visits.visitid = items.itemvisitid
WHERE visits.personid = 10
GROUP BY visitid, visitdate, visitreason
You might want to read this to know GROUP_CONCAT :
How to use GROUP_CONCAT in a CONCAT in MySQL
The document of GROUP_CONCAT() is here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
hope this helps.
SELECT `visits`.`visitid`, `visits`.`visitdate`, `visits`.`visitreason`,`items`.`itemname`, `items`.`itemtime` from `visits` INNER JOIN `items` ON `visits`.`personid`=`items`.`personid` WHERE `visits`.`personid` = '10' ORDER BY `visits`.`visitdate` DESC
if there is any error please change the field name personid in 'items' table.and then check.
This query:
SELECT v.visitid,
v.visitdate,
v.visitreason,
i.itemid,
i.itemname,
i.itemtime
FROM visits v
INNER JOIN items i
ON ( v.visitid = i.itemvisitid )
WHERE v.person_id = 10
ORDER BY v.visitdate DESC,
i.itemid ASC
will link both tables and produce a resultset that you can traverse using a double loop. The outer loop to process changes to the visit, and the inner to add every item visited in a particular visit.

sum two different table and the subtract its total

i have this problem with me on how to sum total value from two different table and then after getting its total i want to subtract it. for example i have table "rsales" and "sales" and i have these ff vlue below.
data from "rsales"
id | total | pcode |
1 | 100 | 2143 |
2 | 100 | 2143 |
3 | 50 | 2222 |
4 | 50 | 2222 |
data from "sales"
id | total | pcode |
7 | 100 | 2143 |
8 | 50 | 2222 |
my problem is this. i want to sum all "total" values from sales and sum "total"value from rsales group by pcode.and then after getting its sum i want to subtract it. my page must be something like this.
total pcode
| 100 | 2143 |
| 50 | 2222 |
i have this ff code but it doesnt wor for me
sql "select sum(rsales.total)- sum(sales.total) as t1 where pcode = rsales.pcode"
Use:
SELECT
SUM(r.total)-(
SELECT SUM(s.total)
FROM sales AS s WHERE r.pcode=s.pcode
) as total,
r.pcode
FROM rsales AS r
GROUP BY r.pcode;
Output:
+--+--+--+--+--+-
| total | pcode |
+--+--+--+--+--+-
| 100 | 2143 |
| 50 | 2222 |
+--+--+--+--+--+-
2 rows in set
Have you tried something like this?
SELECT
SUM(L.total) as lTotal, SUM(R.total) as rTotal
FROM
sales L
INNER JOIN rsales R
ON
R.pcode = L.pcode
GROUP BY L.pcode
If you get expected values from both tables you can easily add Additions and Subtruction in FROM clause.
There's no joins needed to do this. This solution works if some pcodes are only in one table:
select SUM(total), pcode from (
select sum(total) as total, pcode from rsales group by pcode
union all
select SUM(-total) as total, pcode from sales group by pcode) salesTables
group by pcode

Selecting 3 Different Tables

I have 3 table named rsales, rreturn, productlist. Table rsales and rreturn has total column. My aim is, I want to sum all total values from rsales and total value from rreturn table and select specific value only from productlist table.
Let say, for example, I have this data from productlist table:
id | pcode | pname | pdesc |
1 | 222 | 33uf | 10v |
data from rsales table:
id | total | pcode |
1 | 200 | 222 |
2 | 200 | 222 |
data from rreturn table:
id | total | pcode |
1 | 50 | 222 |
2 | 20 | 222 |
the output must be something like this:
id | pcode | pname | pdesc | total
1 | 222 | 33uf | 10v | 470
My question is this: I want to sum all total values from rsales and rreturn table and select all values from productlist. I have this following code below and runs very well. But it can only sum total value from rsales table, or rather, it can only sum value from one single table.
$result = mysql_query("SELECT
productlist.*,
SUM(rsales.total) as total,
SUM(rsales.vatable_sales) as vatable_sales,
SUM(rsales.vats) as vats,
SUM(rsales.discount) as discount
FROM productlist
LEFT JOIN rsales ON rsales.pcode = productlist.pcode
GROUP BY pcode
ORDER BY total ASC");
You are not very explicit about what doesn't work for you. Are you getting results, but the numbers are not what you are expecting? Well, say you have three sales and two returns, then you get six records by joining all tables. Thus you double your sales and triple your returns.
You can join aggregations instead, for one solution:
$result = mysql_query("SELECT
productlist.*,
sumsales.sum_total + sumreturns.sum_total as total,
sumsales.sum_vatable_sales as vatable_sales,
sumsales.sum_vats as vats,
sumsales.sum_discount as discount
FROM productlist
LEFT JOIN
(
SELECT pcode, SUM(total) as sum_total, SUM(vatable_sales) as sum_vatable_sales, SUM(vats) as sum_vats, SUM(discount) as sum_discount
FROM rsales
GROUP BY pcode
) AS sumsales ON sumsales.pcode = productlist.pcode
LEFT JOIN
(
SELECT pcode, SUM(total) as sum_total
FROM rreturn
GROUP BY pcode
) AS sumreturns ON sumreturns.pcode = productlist.pcode
ORDER BY total ASC");

Categories