当列为varchar时,django从mysql中选择最大字段
使用 Django 1.1,我尝试从 varchar 列(在 MySQL 中)中选择最大值。该列中存储的数据如下所示:
9001
9002
9017
9624
10104
11823
(实际上,数字比这个大得多。)
这一直有效,直到数字增加到 10000 以上:
Feedback.objects.filter(est__pk=est_id).aggregate(sid=Max('sid'))
现在,同一行将返回 9624 而不是 11823。
我可以直接在数据库中运行查询,这给了我我需要什么,但我无法找出在 Django 中执行此操作的最佳方法。查询将是:
select max(sid+0) from Feedback;
任何帮助将不胜感激。
谢谢!
Using Django 1.1, I am trying to select the maximum value from a varchar column (in MySQL.) The data stored in the column looks like:
9001
9002
9017
9624
10104
11823
(In reality, the numbers are much bigger than this.)
This worked until the numbers incremented above 10000:
Feedback.objects.filter(est__pk=est_id).aggregate(sid=Max('sid'))
Now, that same line would return 9624 instead of 11823.
I'm able to run a query directly in the DB that gives me what I need, but I can't figure out the best way to do this in Django. The query would be:
select max(sid+0) from Feedback;
Any help would be much appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本着“任何帮助将不胜感激”的精神,您应该弄清楚为什么它在 Django 中停止工作(但显然不在 MySQL 中) - 在 10,000 时。
正在生成的查询是什么?请参阅此问题了解如何找到该问题出去。
我怀疑这是因为您添加了 +0 以使查询中的排序数字。我不认为 Django 自然支持这一点,所以你有两个选择:
In the spirit of "any help would be much appreciated", you should figure out why it stopped working inside Django (but apparently not inside MySQL) - at 10,000.
What is the query that is being generated? See this question for how to find that out.
I suspect it is because you're adding the +0 to make the sort numeric in your query. I don't think Django supports this naturally, so you have two options: