UPDATE products SET stock = 0 WHERE barcode IN ( SELECT fridge.barcode FROM fridge WHERE fridge.amount < fridge.quantity );
我知道这并不能完全回答这个问题,但不需要两个SQL语句.
在python中执行此操作:
import MySQLdb def order(): db = MySQLdb.connect(host='localhost', user='root', passwd='$$', db='fillmyfridge') cursor = db.cursor() cursor.execute('select barcode from fridge where amount < quantity') db.commit() rows = cursor.fetchall() for row in rows cursor.execute('update products set stock = 0 where barcode = %s', row[0])