[英]how to update a table and insert a new record to another table in one query?
I have two table in mysql that is product and sell. 我在mysql中有两个表,即产品和销售。 The product contain the stock and the sell contain the purchasing record.
产品包含库存,销售包含购买记录。 Now i want to sell a product from the stock when i insert the quantity of selling item into the sell table then the same amount should be decrease in the stock that is product table.
现在,当我在销售表中插入一定数量的销售商品时,我想从库存中出售产品,那么应该减少作为产品表的库存中的相同数量。 Please help me for the query.
请帮我查询。
Sell table insertion. 卖表插入。
$cus_id = $_POST['customer'];
$reg_id = $_POST['cus_region'];
$cat_id = $_POST['product'];
$name = $_POST['name'];
$price = $_POST['unit_price'];
$quantity = $_POST['quantity'];
$date = $_POST['datepicker'];
$total = $price * $quantity;
$payment = $_POST['payment'];
$remainig = $total - $payment;
$query = "INSERT INTO sell SET cus_id = '".$cus_id."',reg_id = '".$reg_id."', cat_id = '".$cat_id."',product = '".$name."',selling_price = '".$price."',selling_date = '".$date."',item_quantity = '".$quantity."',amount_paid= '".$payment."',total_price = '".$total."', remaining = '".$remainig."'"; $ query =“插入插入出售SET cus_id ='”。$ cus_id。“',reg_id ='”。$ reg_id。“',cat_id ='”。$ cat_id。“',product ='”。$ name。“ ',selling_price ='“。$ price。”',selling_date ='“。$ date。”',item_quantity ='“。$ quantity。”',amount_paid ='“。$ payment。”',total_price ='“ 。$ total。“'',剩余='”。$ remainig。“'”;
$res = mysql_query($query); $ res = mysql_query($ query);
Two way to resolve that, 解决该问题的两种方法
1) if you are getting the quantity of product you can make second query to deduct the quantity from stack table (product table) 1)如果您要获取产品数量,则可以进行第二次查询以从堆栈表(产品表)中扣除数量
2) You can use trigger for such operations, while you insert in to sales table that same quantity you can reduce from the product table 2)您可以使用触发器进行此类操作,同时在销售表中插入可以从产品表中减少的数量
You have to just change or decrease the quantity of the product in PRODUCT table. 您只需要在PRODUCT表中更改或减少产品数量即可。 So, you have to use UPDATE query.
因此,您必须使用UPDATE查询。
This can solve your issue: 1) I consider that your cat_id
is the unique product key to identify a product in your PRODUCT table. 这可以解决您的问题:1)我认为您的
cat_id
是在PRODUCT表中标识产品的唯一产品密钥。
2) I consider that you have quantity
column in PRODUCT table, which store the quatity of the product. 2)我认为您在PRODUCT表中有
quantity
列,该列存储产品的数量。 And 1 product is being sold at a time. 一次出售一种产品。
UPDATE TABLE_NAME
SET quantity
= quantity
- 1 WHERE cat_id = "SOME_INT_ID_WITHOUT_QUOTES" UPDATE
TABLE_NAME
SET quantity
= quantity
-1 WHERE cat_id =“ SOME_INT_ID_WITHOUT_QUOTES”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.