开发者

Postgres sorting problem

I want to sort by rating DESC. It works with MySQL but on PostgreSQL. I get a different result.

You can see the problem here: http://www.vinderhimlen.dk/开发者_运维知识库konkurrencer

My controller:

  def sort_column
    Konkurrancer.column_names.include?(params[:sort]) ? params[:sort] : "rating"
  end

  def sort_direction
    %w[desc asc].include?(params[:direction]) ? params[:direction] : "desc"
  end


Not sure what your issue is exactly or how it "doesn't work", from lack of details in your question. But at least two factors can affect sorting in such a way that you'd get different results in MySQL and PostgreSQL.

The first is collation. In particular if you're playing with 9.1 beta. Last I installed MySQL (which was a while ago, so they might have fixed this since), it was collating things as latin-1/swedish by default, vs utf-8/english for PostgreSQL.

The other is nulls. MySQL always places these last if memory serves. By contrast, PostgreSQL consistently places them at the end of btree indexes, and thus places them last when ordering asc and first when ordering desc. You can change this behavior by ordering using nulls first/nulls last.

In your particular case, my guess is that you want to order by rating desc nulls last, as opposed to the default behavior which will place nulls first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜