PostgreSql能不能返回固定行数的结果集?比如要返回10行记录,但是符合条件的只有5条,则另外5条记录用空行填充。再不创建临时表的情况下,有没有支持这样的SQL语句。

解决方案 »

  1.   


    t_girl=# \d t;
                                 Table "public.t"
      Column  |     Type      |                   Modifiers                    
    ----------+---------------+------------------------------------------------
     id       | integer       | not null default nextval('t_id_seq'::regclass)
     username | character(20) | not null
    Indexes:
        "t_pkey" PRIMARY KEY, btree (id)t_girl=# select * from t;
     id |       username       
    ----+----------------------
      1 | wangwei             
      2 | meimei              
      3 | Hunter Valley       
      4 | I love              
      5 | shizhu              
      6 | shicui              
      7 | cuihua              
      8 | wanli               
      9 | shi4zhu             
     10 | sh4icui             
    (10 rows)t_girl=# select a.* from t as b full join (select * from t limit 5) as a using(id);
     id |       username       
    ----+----------------------
      1 | wangwei             
      2 | meimei              
      3 | Hunter Valley       
      4 | I love              
      5 | shizhu              
        | 
        | 
        | 
        | 
        | 
    (10 rows)t_girl=#