获取mysql DB列以列出

时间:2019-01-11 01:28:33

标签: python list

我必须将2个列值提取到2个单独的列表中,我在下面尝试过,但是将相同的列值放入两个列表中

    mycursor = mydb.cursor()
mycursor.execute("SELECT ORDERS,ORDERDATE FROM demo.ORDERS WHERE ORDERSTATUS 
    = 'returned';")    
    myresult = mycursor.fetchall()
    order= [row[0] for row in myresult]
    ORDERDATE= [row[0] for row in myresult]
    print (order)     

可以请教吗。

谢谢 Yatan

1 个答案:

答案 0 :(得分:0)

迭代结果集并填充两个列表:

mycursor = mydb.cursor()
sql = "SELECT ORDERS, ORDERDATE FROM demo.ORDERS WHERE ORDERSTATUS = 'returned';"
mycursor.execute(sql)  
myresult = mycursor.fetchall()
order = []
orderdate = []

while myresult:
    for row in myresult: 
        order.insert(len(order), row[0])
        orderdate.insert(len(orderdate), row[1])