如何使用两个字段进行排序?
我有一个排序/分组问题,希望有人可以补充一些见解。
我们有一个故事表,其中包含发布日期和更新日期。我正在使用 Django,所以它看起来像这样:
class Story(models.Model):
pub_date = models.DateTimeField(db_index=True)
update_date = models.DateTimeField(blank=True, null=True, db_index=True)
headline = models.CharField(max_length=200)
...
我们希望在按天分组的分页页面上显示故事。所以...
Jan 20
Story 1
Story 2
Jan 19
Story 1
Story 3
挑战是,如果一个故事有 update_date,它应该显示两次,一次在 pub_date 日,一次在 update_day 日期(例如故事 1)。
有成千上万个故事,所以我当然不能用 python 完成所有这些,但我不知道如何在 SQL 中执行此查询。
我现在所拥有的是按 -pub_date 对所有内容进行排序,然后获取给定页面上的最大和最小日期范围。然后,我使用 update_date 查询这些日期之间的任何故事,并在 python 中将它们组合和分组。问题是页面上的项目数量是不规则的。
所以我想我的问题是这样的:查询表中的项目列表并根据两个字段对它们进行排序的最佳方法是什么,如果它在第二个字段中有值,则在查询中复制一个项目,然后根据在两个领域?
希望这是有道理的...
I have a sorting/grouping issue that I'm hoping somebody could add some insight on.
We have a table of stories that have a publish date and an updated date. I'm using Django so it looks like this:
class Story(models.Model):
pub_date = models.DateTimeField(db_index=True)
update_date = models.DateTimeField(blank=True, null=True, db_index=True)
headline = models.CharField(max_length=200)
...
We want to display the stories on a paginated page grouped by day. So...
Jan 20
Story 1
Story 2
Jan 19
Story 1
Story 3
The challenge is that if a story has an update_date it should be displayed twice, once on the pub_date day, and once on the update_day date (e.g. Story 1).
There are 10s of thousands of stories so I can't do it all in python of course, but I don't know of a way to do this query in SQL.
What I have right now is sorting everything by -pub_date and then getting a range of the max and min dates on a given page. I then query for any stories between those dates with an update_date and combine and group them in python. The problem is that the number of items on a page is irregular then.
So I guess my question is this: What is the best way to query a table for a list of items and sort them based on two fields, duplicating an item in the query if it has a value in the second field, and then sorting based on the two fields?
Hope that makes sense...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只能想到“联盟”能够做到这一点。
这是一个示例。不确定数据库经常向数据库发送此类查询有多快或多好 D:
查询假设您的表名称是 stories,并使用列 headline strong>、pub_date 和 update_date。它还假设尚未更新的故事在 update_date 列中具有 null 值。
如果要向查询添加限制,应该在“order by”子句之后最后完成。
i can only think of "union" being able to do this.
here's an example of what that would look like. not sure how fast or good it is for the database to have this type of query sent to it often though D:
the query assumes your table name is stories, and uses the columns headline, pub_date and update_date. it also assumes that a story that hasn't been updated has the value null in the update_date column.
if you want to add a limit to the query, it should be done last, after the "order by" clause.
你的问题和我的很相似。我从 Facebook 墙上读到了一些文章。我有两次约会,一次是关于项目创建(用户发布该项目),一次是关于项目检索(我从 Facebook 读取该项目)。我想显示今天发布或检索的项目。
编辑:我对这句话过于乐观:这是错误的。
your question is similar to what I had. I read some items from Facebook walls. I had two dates, one on item creation(user posts the item), one on item retrieval(I read the item from Facebook). I wanted to show items that are posted or retrieved today.
Edit: I was over optimistic in this sentence: It is wrong.
对列名进行一些假设,您需要 UNION ALL 来保留两个部分的重复项。
Making some assumptions on the column names, you need UNION ALL to retain duplicates from both parts.