[英]How to update Spark DataFrame Column Values of a table from another table based on a condition using Pyspark
[英]SQL /peewee. How do I update a record based on condition from another table?
因此,我正在尝试在购买商品后更新商店。 这些是我正在使用的模型。
class Cart():
user = pw.ForeignKeyField(User, backref="cart_user", null=True)
item = pw.ForeignKeyField(
Item, backref="cart_item", null=True, on_delete='SET NULL')
payment_status = pw.BooleanField(null=False, default=False)
payment = pw.ForeignKeyField(Payment, backref="payment", null=True)
amount = pw.IntegerField(null=False, default=1)
class Item():
name = pw.CharField(null=False)
product_type = pw.CharField(null=False)
size = pw.CharField(null=False)
color = pw.CharField(null=False, default=False)
price = pw.IntegerField(null=False)
image = pw.CharField(null=False)
stock = pw.IntegerField(null=True)
购买后,我想从库存价值中减去购物车金额的价值。
item = (Item.update(stock=20)
.where(Item.id.in_
(Cart.select(Cart.item_id)
.where((Cart.user == current_id) & (Cart.payment_status == False)))))
item.execute()
我被困在这里了。 我只能硬编码股票价值。 我不确定如何从 Item.stock 中减去 Cart.amount
ok 终于找到解决办法了
cart_amount = Cart.select(Cart.amount).where(Cart.item_id == Item.id)
item = (Item.update(stock=Item.stock - cart_amount)
.where(Item.id.in_
(Cart.select(Cart.item_id)
.where((Cart.user == current_id) & (Cart.payment_status == False)))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.