python - MySQLdb的SQL语句问题

查看:109
本文介绍了python - MySQLdb的SQL语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

import MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8")
cursor = conn.cursor()

n = cursor.execute("select * from studentlist")
for row in cursor.fetchall():
    for r in row:
        print r
        

像这种select * from studentlist 可以直接查询

但是如果我要换为下面类似语句,就会出现错误。

SELECT
    student_statistics_day.stat_day AS `日期`,
    studentlist.`name` AS `姓名`,
    studentlist.phone AS `手机`,
    studentlist.class AS `班级`,
    student_statistics_day.online_time AS `上课时间`,
    student_statistics_day.level_name AS `级别`
FROM
studentlist
LEFT JOIN student_statistics_day ON studentlist.student_id = student_statistics_day.student_id
LEFT JOIN student_day ON student_statistics_day.student_id = student_day.student_id
WHERE
    student_statistics_day.level_name = "优秀"
OR student_statistics_day.level_name = "良好"
ORDER BY
    `studentlist` DESC

解决方案

这种复杂查询语句建议用三引号"""包裹, 试试看, 没准就欧了

这篇关于python - MySQLdb的SQL语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆