繁体   English   中英

为什么我收到此错误:_mysql_exceptions.OperationalError:(1241,'Operand应该包含1列')

[英]Why am I receiving this error: _mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)')

我收到上述错误,我认为这是在向表中添加行时传递的类型错误。 但是,我正在使用其他两个表中的列创建新表,并使用与原始表中指定的类型相同的类型。 我想念什么?

首先,我创建表:

cur = db.cursor()
cur.execute("DROP TABLE IF EXISTS lens_items;")
query = '''
CREATE TABLE lens_items (
    solution_id INTEGER(9),
    task_id BIGINT(20),
    item_oid CHAR(48),
    influence DOUBLE
);
'''
cur.execute(query)

接下来,我通过遍历查询并将行结果添加到新表(lens_items)中来创建新表。 我在下面的“ ###### >>>>”处插入:

cursor1 = db.cursor(MySQLdb.cursors.DictCursor)  # a dictcursor enables a named hash
cursor2 = db.cursor(MySQLdb.cursors.DictCursor)  # a dictcursor enables a named hash

query = """
    Select user_id, solution_id 
    From user_concepts
    Where user_id IN (Select user_id FROM fields);
"""  

cursor1.execute(query)

rows1 = cursor1.fetchall()
######>>>>>

for row in rows1:
    user_id = row["user_id"]
    solution_id = row["solution_id"]

    cursor2.execute("""
        INSERT INTO lens_items (solution_id, task_id, item_oid, influence)
        VALUES (solution_id, (SELECT task_id, item_oid, influence
        FROM solution_oids 
        WHERE task_id = %s
        ORDER BY influence DESC));
    """, (solution_id,))

您不遵循INSERT的mysql语法,因为您尝试将values子句与select混合使用。 使用INSERT ... SELECT ...代替:

    INSERT INTO lens_items (solution_id, task_id, item_oid, influence)
    SELECT solution_id, task_id, item_oid, influence
    FROM solution_oids 
    WHERE task_id = %s
    ORDER BY influence DESC

但是,如果在select部分中使用了join,则可以简化数据传输,而将python完全排除在图片之外:

    INSERT INTO lens_items (solution_id, task_id, item_oid, influence)
    SELECT s.solution_id, s.task_id, s.item_oid, s.influence
    FROM solution_oids s
    INNER JOIN user_concepts u ON s.solution_id=u.solution_id
    INNER JOIN fields f ON f.user_id=u.user_id
    ORDER BY influence DESC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM