pg:左边连接在表上首先放置nil记录

时间:2015-05-31 14:23:01

标签: sql postgresql

这是我的项目表:

 Column   |            Type             |                       Modifiers                       | Storage  | Stats target | Description 
------------+-----------------------------+-------------------------------------------------------+----------+--------------+-------------
 id         | integer                     | not null default nextval('projects_id_seq'::regclass) | plain    |              | 
 name       | character varying(255)      |                                                       | extended |              | 
 repo_id    | integer                     |                                                       | plain    |              | 
 created_at | timestamp without time zone |                                                       | plain    |              | 
 updated_at | timestamp without time zone |                                                       | plain    |              | 
 user_id    | integer                     |                                                       | plain    |              | 
 data_path  | character varying(255)      |                                                       | extended |              | 
 private    | boolean                     | default false                                         | plain    |              | 
 uniqueurl  | character varying(255)      |                                                       | extended |              | 
 urlbase    | character varying(255)      |                                                       | extended |              | 
 ancestry   | character varying(255)      |                                                       | extended |              | 
 deleted_at | timestamp without time zone |                                                       | plain    |              | 
Indexes:
    "projects_pkey" PRIMARY KEY, btree (id)
    "index_projects_on_name_and_user_id" UNIQUE, btree (name, user_id)
    "index_projects_on_ancestry" btree (ancestry)
    "index_projects_on_deleted_at" btree (deleted_at)
Has OIDs: no

这是我的rating_cache表:

 Column     |            Type             |                         Modifiers                          | Storage  | Stats target | Description 
----------------+-----------------------------+------------------------------------------------------------+----------+--------------+-------------
 id             | integer                     | not null default nextval('rating_caches_id_seq'::regclass) | plain    |              | 
 cacheable_id   | integer                     |                                                            | plain    |              | 
 cacheable_type | character varying(255)      |                                                            | extended |              | 
 avg            | double precision            | not null                                                   | plain    |              | 
 qty            | integer                     | not null                                                   | plain    |              | 
 dimension      | character varying(255)      |                                                            | extended |              | 
 created_at     | timestamp without time zone |                                                            | plain    |              | 
 updated_at     | timestamp without time zone |                                                            | plain    |              | 
Indexes:
    "rating_caches_pkey" PRIMARY KEY, btree (id)
    "index_rating_caches_on_cacheable_id_and_cacheable_type" btree (cacheable_id, cacheable_type)
Has OIDs: no

我正在使用以下两个表进行左外连接:

SELECT  "projects".* FROM "projects" LEFT OUTER JOIN rating_caches
ON rating_caches.cacheable_id = projects.id 
WHERE "projects"."deleted_at" IS NULL  ORDER BY rating_caches.avg desc

这正确地命令项目(最高平均值先到先)但是在rating_caches表中没有匹配记录的项目甚至在最高平均值之前。例如: 项目:

id  name
1   A
2   B
3   C

rating_caches:

id cacheable_id avg
1  3            3.0
2  2            2.5

查询结果如下:

id name
1  A
3  C
2  B

id = 1的项目不应该持续吗?

1 个答案:

答案 0 :(得分:2)

只需使用NULLS LAST

ORDER BY rating_caches.avg desc NULLS LAST

作为备注,documentation指定:

  

NULLS FIRSTNULLS LAST选项可用于确定   是否在排序中的非空值之前或之后出现空值   排序。默认情况下,null值排序为大于任何非null值   值;也就是说,NULLS FIRSTDESC订单的默认设置,否则为NULLS LAST