在另一个SQL查询中使用python程序中的sql查询结果

mar*_*oln 0 python sql

对不起我上一个非常模糊的问题,但我想如果我得到这个问题的答案,我可以解决它.在下面的程序中,我选择了数量少于数量的产品条形码.我想说,如果条形码(在冰箱表中)与另一个表(产品)中的条形码匹配,则将库存字段设置为0.我遇到的问题是程序试图匹配它找到的所有条形码在对产品表中的单个条形码的查询中(这就是我的想法).有谁知道该怎么做.太感谢了.林肯.

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()
    row = cursor.fetchall()
    cursor.execute('update products set stock = 0 where barcode = %s', row)
Run Code Online (Sandbox Code Playgroud)

Ada*_*eck 5

UPDATE products SET stock = 0 WHERE barcode IN ( 
    SELECT fridge.barcode FROM fridge WHERE fridge.amount < fridge.quantity );
Run Code Online (Sandbox Code Playgroud)

我知道这并不能完全回答这个问题,但不需要两个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])
Run Code Online (Sandbox Code Playgroud)