简体   繁体   English

如何在一个查询中更新表并将新记录插入到另一个表中?

[英]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.

相关问题 查询将一条记录(ID)从一张表插入到另一张表 - Query for insert one record (ID) from one table to another table 如何在单个表中插入一个表并在另一个表中更新? - How to INSERT in one table and UPDATE in another in single QUERY? 如果另一个表中没有记录,则插入到一个表中 - Insert into one table if no record exists in another table 从两个表中各选择一个记录。 通过将另一个值与新表匹配来更新一个值 - select one record each from two table. update the one value by matching the another one to a new table 将记录插入另一个表 - Insert record into another table 查询用另一个表中的另一个随机记录更新记录? - query to update record with another random record from another table? 将数据插入一个表并更新另一个表 - Insert data into one table and update another 尝试创建 Laravel 查询以选择,如果记录不存在则插入,如果存在于另一个表中则更新 - Trying to create laravel query to Select then Insert if record not exist, Update if exist in another table 如何从一个表中获取现有记录的 ID 并将其与其他记录一起插入到另一个表中? - How to grab an ID of an existing record from one table and insert it along with other records into another table? 更新一个表,其中记录在另一个表中找到 - Update one table where record found in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM