update all values from different table that has same id - php

I have two tables named sales and rsales
I have this ff value for table sales
id | pcode | total | discount |
2 | 33 | 100 | 20 |
3 | 33 | 100 | 20 |
and i have this ff value for table rsales
id | pcode | total | discount | sales_id |
4 | 33 | 100 | 20 | 1 |
5 | 33 | 100 | 20 | 2 |
6 | 33 | 100 | 20 | 3 |
My problem is that when I update all values from table sales, the table rsales must be update either if sales.id is equal to sales_id.
so for example if I have updated table sales with id = 2 and 3, the sales_id 2 and 3 from rsales must be updated either.
take note : only 2 and 3 because only that ids is found in table sales ids.
I have this ff codes so far to update table rsales. but the output is shown not as what I meant. it update all values.
mysql_query("UPDATE sales AS t1, rsales AS t2
SET t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE t1.pcode = '$pcode'");

You should add to the WHERE class the common column between the tables.
UPDATE sales AS t1, rsales AS t2
SET
t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE
t1.id = t2.sales_id AND
t1.pcode = '$pcode'
Note: I haven't tested it.

Related

Sum of column is not working in join table PHP

I am trying to calculate the req_qty and in_qty from table1 and table2.
I am trying to fetch the joined 3 tables data SUM(req_qty), SUM(in_qty). Issue come if req.qty not save in table2 it will not works. I am left joining the table by this sku's ids
I made here a very simple table for calculating the sum of the column. generally, the sum of the column is multiplying the total rows.
Please don't close the post and guild for other thread. for my doubt clear i made here a table which I can understand. please help.
table1
ID | sku_1 | req_qty | trans_id
----------------------------------
1 | 123 | 150 | 12345
2 | 142 | 200 | 256314
3 | 123 | 100 | 896523
table2
ID | sku_2 | in_qty | trans_key
-----------------------------------
1 | 142 | 50 | 002563
table3
ID | sku_code | sku_name
--------------------------
1 | 142 | ABC
2 | 123 | XYZ
Expected Output
ID | sku | sku_name | reqQty | inQty
------------------------------------
1 | 123 | XYZ | 250 | 0
2 | 142 | ABC | 200 | 50
Edit to select data when table1 and table2 are empty
SELECT table1.id, table3.sku_code as sku, table3.sku_name,
sum(table1.req_qty) as reqQty, sum(table2.in_qty) as inQty
FROM table3
LEFT JOIN table1 on table3.sku_code = table1.sku_1
LEFT JOIN table2 on table3.sku_code = table2.sku_2
GROUP BY table1.id, table3.sku_code, table3.sku_name
Explanation
You can see an explanation on how left join works here https://www.w3schools.com/sql/sql_join_left.asp#:~:text=The%20LEFT%20JOIN%20keyword%20returns,if%20there%20is%20no%20match.
But to explain this query quickly, we will select all data from table3, left join will find all records from left table (here table3) and mathcing records from right tables (here table2 and table 1).

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;

Mysql query turns non-affected to 0

This is a simple query that when executed updates the cash available by some users when their loans are due:
$sqlx = "UPDATE competitions
SET cash = (SELECT cash_after_deduct
FROM (SELECT l.competitions_id,
(c.cash-l.due_amount) AS cash_after_deduct
FROM loans l
JOIN competitions c ON l.competitions_id = c.id
WHERE l.due_date='2018-10-28'
GROUP BY l.competitions_id) q1
WHERE q1.competitions_id = competitions.id
)
";
The cash row of those users with a loan due on 2018-10-28 should be modified. And it works; however the cash row in users with different due dates are reset to 0, while they should remain unaffected.
Any idea on what can be wrong?
Many thanks in advance.
Your UPDATE query has no criteria to limit the scope of the update to only those affected by the due_date.
Additionally since you are not using an aggregate function on the loans table, MySQL is free to choose ANY single value from those that were grouped. This can lead to unexpected results.
To resolve the issue you can change the SET subquery to a JOIN. This will prevent unmatched records from the loans table from updating the competitions table with 0's. As well as reduce the number of queries that would need to occur, since using a subquery in the SET would require a query to be issued for each record in order to match the current row in competitions that is being updated by set.
Example: http://sqlfiddle.com/#!9/2a2c147/1
UPDATE competitions AS c
INNER JOIN (
SELECT l.competitions_id, SUM(l.due_amount) AS total_due
FROM loans AS l
WHERE l.due_date = '2018-10-28'
GROUP BY l.competitions_id
#optionally limit scope to those that have an amount due
#HAVING total_due > 0
) AS d
ON d.competitions_id = c.id
SET c.cash = c.cash - d.total_due
Data Set
competitions
---
| id | cash |
|-----|------|
| 1 | 5.00 |
| 2 | 2.00 |
| 3 | 0.00 |
loans
---
| id | competitions_id | due_amount | due_date |
|----|-----------------|------------|------------|
| 1 | 1 | 1.00 | 2018-10-19 |
| 2 | 1 | 1.00 | 2018-10-28 |
| 3 | 2 | 1.00 | 2018-10-28 |
| 4 | 1 | 1.00 | 2018-10-28 |
| 5 | 3 | 1.00 | 2018-11-19 |
Result
| id | cash | total_due | cash_after_deduction | loan_deductions |
|----|------|-----------|----------------------|-----------------|
| 1 | 5 | 2 | 3 | 2 |
| 2 | 2 | 1 | 1 | 1 |
This works by first retrieving the competitions_id and due_amount values from the loans table that are affected by the due_date.
The base competitions table updates are then limited by the INNER JOIN, which includes only the records that match those found within the loans table.
I used SUM as the aggregate function to ensure all of the due_amount records from all the loans for the competitions_id are totaled.
This looks like what you had intended, if not and you want a single due_amount, the query can be modified to match your desired results.

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

update query with specific data

I have two tables: sales and productlist. Let's say I have these ff value for its table
This is the ff value of sales
id | pcode | qty_sold |
1 | 123 | 20 |
2 | 123 | 20 |
This is the ff value for productlist
id | pcode | pleft |
6 | 123 | 20 |
7 | 333 | 40 |
My problem is this: When I update 1 product from productlist it updates properly with the product I want to update, but other product data is turned to 0. The expected value when updating the productlist must be look like this:
id | pcode | pleft |
6 | 123 | 60 |
7 | 333 | 40 |
I have this code so far:
mysql_query("UPDATE productlist SET pleft=pleft+(SELECT SUM(qty_sold) ".
"FROM sales WHERE sales.pcode=productlist.pcode)");
But it displays like this:
id | pcode | pleft |
6 | 123 | 60 |
7 | 333 | 0 |
i guess you missed WHERE clause in your UPDATE statement, like:
UPDATE productlist
SET pleft=pleft+(SELECT SUM(qty_sold) FROM sales WHERE sales.pcode=productlist.pcode)
WHERE productlist.pcode = "your_id";
You are only using WHERE for your inner query.
You have to use WHERE for outer query for updating particular record.
UPDATE productlist
SET pleft=pleft+(SELECT SUM(qty_sold) FROM sales WHERE sales.pcode=productlist.pcode)
WHERE productlist.pcode = "id";
The reason you get '0' is, Your select returns '40' for first ID but no rows returned for second. so it is considered as null. Thats y you get '0' for second id. Try using l ifnull(select,0) function.
UPDATE productlist SET pleft=pleft+ifnull((SELECT SUM(qty_sold)
FROM sales WHERE sales.pcode=productlist.pcode),0)

Categories