-1

i have 2 tables named "sales" and "rsales" ,

and now i have this ff data from "sales"

id | pcode | total | 
2  |  122  |  20   |  
3  |  122  |  20   | 
4  |  321  |  30   |
5  |  321  |  30   |

don't wonder why i have duplication of pcode,total,discount, it's simply because when i "add order" or click my submit button it saves to the tble "sales" in that way like what i have illustrated above. i have this code to share with you how to update my tbl "sales" and it works very well. what i did is that i get the id. let say for example i get the id "1" from "sales" so when i run my query below it update id "1" but also id "2" because they are the same "pcode" it's obvious to my query. i have no problem updating my tbl "sales"

mysql_query("UPDATE sales SET total = '$total_discount' where pcode = '$pcode' "); 

so my problem is this i have this ff codes to update my tbl "rsales

   mysql_query("UPDATE rsales SET total = '$total_discount' ,discount = '$tot'  WHERE   rsales.sales_id IN (SELECT sales.id FROM sales)");

what i want is that when i update specific pcode in my tbl "sales" the tbl "rsales" must be update also. so let say for example i update "total" from "sales" by pcode 122., so from 20 i change it to 40 , so my tbl "rsales" must be look like this way

id | pcode | total | sales_id|
2  |  122  |  40   |  2      |
3  |  122  |  40   |  3      |
4  |  321  |  30   |  4      |
5  |  321  |  30   |  5      |

butmy query is showing me this result. allvrows updating.i can't update exactly the "total" from "rsales" pls help me. every help is very helpful.

id | pcode | total | sales_id|
2  |  122  |  40   |  2      |
3  |  122  |  40   |  3      |
4  |  321  |  40   |  4      |
5  |  321  |  40   |  5      |
4

1 回答 1

0

在下面试试这个。另外,根据您的表格布局,您缺少该discount列,我假设它在测试数据库中。

UPDATE sales AS t1, rsales AS t2
SET t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE t1.pcode = '$pcode'
AND t2.pcode = t1.pcode

顺便说一句,由于这一行,您正在修改所有行:

WHERE rsales.sales_id IN (SELECT sales.id FROM sales)

在这里,您说要更新IN(其中一个)字段的rsales位置。你正在全力以赴。如果您需要具体指定什么 ID,请将其添加到上述查询的底部:sales_idsales.idsales.id

AND t1.id = '$id'
AND t2.sales_id = t1.id

还没有模拟你的数据库,所以我没有测试它。

于 2013-08-31T17:03:27.487 回答