代码之家  ›  专栏  ›  技术社区  ›  Elvira

sql占位符行

  •  1
  • Elvira  · 技术社区  · 8 年前

    我有一个顶点项目 P_USERS 它的值可以大于从下面的查询返回的行数。

      select 
           first_name,
           last_name
      from  accounts
      where account_role = 'Author'
      order by account_nr;
    

    我希望将占位符行添加到查询中( first_name = null , last_name = null 如果查询中的总行数小于 apex_item .

    有什么秘诀可以帮助你实现这个目标吗?也许用一个 LEFT join ?

    2 回复  |  直到 8 年前
        1
  •  1
  •   J. Chomel    8 年前

    如果你定义了一个大于最小值的并集,那么你必须加上剩余的。

    以下是您可以尝试适应您的情况:

      SELECT i,c FROM  (
    select rownum i, c from (
    select  'a' c from dual union all select 'b' from dual union all select 'd' from dual union all select 'be' from dual
    )), (Select Rownum r From dual Connect By Rownum <= 3)
    where (i(+)= r)
    union select i,c from (select rownum i, c from (
    select  'a' c from dual union all select 'b' from dual union all select 'd' from dual union all select 'be' from dual
    )) where i>3
    
        2
  •  1
  •   J. Chomel    8 年前

    您可以尝试使用 LEFT JOIN .

    首先,创建一个数字列表,直到达到您想要的限制 suggested here :

    -- let's say you want 300 records
    Select Rownum r From dual Connect By Rownum <= 300
    

    然后,您可以使用此方法左连接并具有空记录:

    SELECT C, R FROM  
      ( select rownum i, c from (select  'a' c from dual union all select 'b' from dual) )
    , ( Select Rownum r From dual Connect By Rownum <= 300)
    where i(+)= r order by r
    

    所以你可以根据你的情况进行调整:

    SELECT F,L FROM  
      ( select rownum i, f, l from (
                 select first_name f, last_name l 
                   from  accounts where account_role = 'Author'
                order by account_nr) )
    , ( Select Rownum r From dual Connect By Rownum <= 300)
    where i(+)= r
    
    推荐文章