数据库排序

3 views
Skip to first unread message

asmcos (SoSE)

unread,
Jan 5, 2012, 10:30:44 PM1/5/12
to uli...@googlegroups.com
定义: post_date = Field(datetime.datetime, auto_now_add=True)

获取数据:
 applys = Apply.all().order_by(Apply.c.post_date)
 applys = Apply.filter(Apply.c.title==settings.SITE.lecture_name)

结果:
1 马健   2012-01-05 20:44:22 
2 zhaodingmao  2012-01-04 05:08:34  
3 shiyibin   2012-01-04 06:39:22  
4 秦川   2012-01-05 00:49:18  

排在第一的是最后报名的。。。。


--
----------------
何家胜
SoSE(so easy)  就这么简单!
website : http://www.jeapedu.com
手机    :137-1860-9533(移动),18010096001 (电信)
MSN    :  asm...@hotmail.com

limodou

unread,
Jan 5, 2012, 10:59:22 PM1/5/12
to uli...@googlegroups.com
2012/1/6 asmcos (SoSE) <asm...@gmail.com>:

> 定义: post_date = Field(datetime.datetime, auto_now_add=True)
>
> 获取数据:
> applys = Apply.all().order_by(Apply.c.post_date)
> applys = Apply.filter(Apply.c.title==settings.SITE.lecture_name)
>
> 结果:
> 1 马健 2012-01-05 20:44:22
> 2 zhaodingmao 2012-01-04 05:08:34
> 3 shiyibin 2012-01-04 06:39:22
> 4 秦川 2012-01-05 00:49:18
>
> 排在第一的是最后报名的。。。。
>
>

.order_by(Apply.c.post_date.desc())

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: http://code.google.com/p/uliweb/
My Blog: http://hi.baidu.com/limodou

asmcos (SoSE)

unread,
Jan 6, 2012, 12:43:32 AM1/6/12
to uli...@googlegroups.com
在 2012年1月6日 上午11:59,limodou <lim...@gmail.com>写道:
2012/1/6 asmcos (SoSE) <asm...@gmail.com>:
> 定义: post_date = Field(datetime.datetime, auto_now_add=True)
>
> 获取数据:
>  applys = Apply.all().order_by(Apply.c.post_date)
>  applys = Apply.filter(Apply.c.title==settings.SITE.lecture_name)
>
> 结果:
> 1 马健   2012-01-05 20:44:22
> 2 zhaodingmao  2012-01-04 05:08:34
> 3 shiyibin   2012-01-04 06:39:22
> 4 秦川   2012-01-05 00:49:18
>
> 排在第一的是最后报名的。。。。
>
>

.order_by(Apply.c.post_date.desc())

没有变化,难道是日期格式问题。

 
--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: http://code.google.com/p/uliweb/
My Blog: http://hi.baidu.com/limodou

limodou

unread,
Jan 6, 2012, 4:20:11 AM1/6/12
to uli...@googlegroups.com
2012/1/6 asmcos (SoSE) <asm...@gmail.com>:

>
>
> 在 2012年1月6日 上午11:59,limodou <lim...@gmail.com>写道:
>
>> 2012/1/6 asmcos (SoSE) <asm...@gmail.com>:
>> > 定义: post_date = Field(datetime.datetime, auto_now_add=True)
>> >
>> > 获取数据:
>> > applys = Apply.all().order_by(Apply.c.post_date)
>> > applys = Apply.filter(Apply.c.title==settings.SITE.lecture_name)
>> >
>> > 结果:
>> > 1 马健 2012-01-05 20:44:22
>> > 2 zhaodingmao 2012-01-04 05:08:34
>> > 3 shiyibin 2012-01-04 06:39:22
>> > 4 秦川 2012-01-05 00:49:18
>> >
>> > 排在第一的是最后报名的。。。。
>> >
>> >
>>
>> .order_by(Apply.c.post_date.desc())
>>
> 没有变化,难道是日期格式问题。
>

哦。那应该用升序,使用asc()

asmcos (SoSE)

unread,
Jan 10, 2012, 10:52:00 PM1/10/12
to uli...@googlegroups.com
这个order_by 还是不对;
是不是因为 SQLAlchemy 版本问题?

SQLAlchemy-0.7.4-py2.6.egg



limodou

unread,
Jan 10, 2012, 11:26:17 PM1/10/12
to uli...@googlegroups.com
2012/1/11 asmcos (SoSE) <asm...@gmail.com>:

> 这个order_by 还是不对;
> 是不是因为 SQLAlchemy 版本问题?
>
> SQLAlchemy-0.7.4-py2.6.egg
>

应该不是。order by 本身有升序,降序,你可以先手工执行sql测试一下。用orm不过是自动生成相应的sql罢了。

asmcos (SoSE)

unread,
Jan 10, 2012, 11:31:53 PM1/10/12
to uli...@googlegroups.com
手工的没有问题。我用mysql命令做实验的,顺序很好。

applys = Apply.all().order_by(Apply.c.post_date.asc())
applys = apply.filter(Apply.c.title==name).filter(Apply.c.status==True)

1.语句 没有写错吧?
2. 和下面这个filter有关系吗?


limodou

unread,
Jan 10, 2012, 11:33:58 PM1/10/12
to uli...@googlegroups.com
2012/1/11 asmcos (SoSE) <asm...@gmail.com>:

> 手工的没有问题。我用mysql命令做实验的,顺序很好。
>
> applys = Apply.all().order_by(Apply.c.post_date.asc())
> applys = apply.filter(Apply.c.title==name).filter(Apply.c.status==True)
>
> 1.语句 没有写错吧?
> 2. 和下面这个filter有关系吗?
>

第二行是apply?和前面的applys没什么关系啊。你可以把
[ORM]
DEBUG_LOG = True

在命令行看生成的sql语句是什么样的。

SeedSeek

unread,
Jan 10, 2012, 11:42:10 PM1/10/12
to Uliweb
用 0.6.7
可以正常排序

新版的沒試過

asmcos (SoSE)

unread,
Jan 11, 2012, 1:32:30 AM1/11/12
to uli...@googlegroups.com
问题找到了;(刚才由于mail换行 将第二行的apply变成了小写。源程序是大写的)。

是第二行结果覆盖了第一行,以前以为先Apply.all()是读取数据库;
再用Apply.filter()将读取的结果进行过滤。

后来发现用filter();直接就能读取并且过滤。。。。


----BUG---
applys = Apply.all().order_by(Apply.c.post_date.asc())
applys = Apply.filter(Apply.c.title==name).filter(Apply.c.status==True)
-----修复后的-----
applys = Apply.filter(Apply.c.title==name).filter(Apply.c.status==True).order_by(Apply.c.post_date.asc())


Reply all
Reply to author
Forward
0 new messages