【问题标题】:Trouble Querying Against int Field using MYSQL使用 MYSQL 查询 int 字段时遇到问题
【发布时间】:2011-02-23 20:27:32
【问题描述】:

嘿, 我正在尝试运行以下查询:

     self.cursor.execute('SELECT courses.courseid, days, starttime, bldg, roomnum, '
                    'area, title, descrip, prereqs, endtime FROM '
                    'classes, courses, crosslistings, coursesprofs, profs WHERE '
                    'classes.courseid = courses.courseid AND '
                    'courses.courseid = crosslistings.courseid AND '
                    'courses.courseid = coursesprofs.courseid AND '
                    'coursesprofs.profid = profs.profid AND '
                    'classes.classid LIKE %s'
                    ';',
                    (self.classid))

classid 是 db 中的 int(11) 字段。当我设置 self.classid = % 时,它会返回所有结果,但是一旦我将其设置为“3454”或其他数量,即使有一个具有该 classid 的类,它也不会返回任何内容。我是否对 int 字段进行了错误的查询?

即使是更简单的查询,例如 从 classes.classid = '3454' 的类中选择 *;不工作

【问题讨论】:

  • 对于整数列,您应该使用= 而不是LIKELIKE 将导致列值和参数转换为字符串并进行比较,这肯定比 int 比较慢。

标签: python mysql sql database


【解决方案1】:

试试:

从 classes.classid = 3454 的类中选择 *;

【讨论】:

    【解决方案2】:

    我自己解决了这个问题。根据我的数据库结构,我查询了错误的字段。我一直在寻找不存在的值,这就是为什么我总是返回一个空的结果集。感谢您对 = 运算符的帮助,但已被使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2018-02-13
      • 1970-01-01
      相关资源
      最近更新 更多