用SQL语句实现乘法口诀

 这个是用 T-SQL  实现的

 Declare  @i Int
         ,@j Int
         ,@Out Varchar(5000)

 Set @i=1
 Set @j=1  
 Set @Out=''

 While @i<=9
  Begin

 While @j<=@i 
      Begin
        Set @Out= @Out + Cast(@j As Varchar(10))+ '×' + Cast(@i As Varchar(10))
                 + '=' + Cast(@j*@i As Varchar(1000))

        If @j*@i>=10
            Set @Out=@Out + ' |'  --1个空格
        Else
            Set @Out=@Out + '  |' --2个空格
 
  Set @j=@j+1       
   End

    Print @Out
    Set @Out=''

--    回车换行
--    Print CHAR(13)+CHAR(10) 

 Set @i=@i+1
    Set @j=1
 End

下面的是别人用ORACLE实现的,等待测试。。。。。

 

1、select rn, ltrim(max(sys_connect_by_path(product, ',')), ',') product
     from
     (
       select rn, product, min(product) over(partition by rn) product_min
       ,(row_number() over(order by rn, product)) + (dense_rank() over(order by rn)) numId
       from
       (
         select b.rn, a.rn || '*' || b.rn || '=' || a.rn * b.rn product
         from (select rownum rn from all_objects where rownum <= 9) a,
        (select rownum rn from all_objects where rownum <= 9) b
        where a.rn <= b.rn
        order by b.rn, product
      )
    )
    start with product = product_min
    connect by numId - 1 = prior numId
    group by rn;

2、select ltrim(sys_connect_by_path
                   (rownum || '*' || lv || '=' || rpad(rownum * lv, 2),'   ')
                 )
       from (select level lv from dual connect by level < 10)
      where lv = 1
     connect by lv + 1 = prior lv;

3、select reverse(sys_connect_by_path
                   (reverse(rpad(rownum * lv, 2)) || '=' || rownum || '*' || lv ,'   ')
                   )
       from (select level lv from dual connect by level < 10)
      where lv = 1
     connect by lv + 1 = prior lv;

4、with t as (select level as n from dual connect by level <=9)
     select max(substr(sys_connect_by_path(b.n || '*' || a.n || '=' || a.n * b.n, ', '),3)) as val
     from t a, t b
     where a.n >= b.n
     start with b.n=1
     connect by a.n=prior a.n and b.n=prior b.n+1
     group by a.n;

5、with t as (select level as n from dual connect by level <=9)
     select substr(sys_connect_by_path(b.n || '*' || a.n || '=' || a.n * b.n, ' '),2) as val
     from t a, t b
     where a.n >= b.n and level = a.n
     start with b.n=1
     connect by a.n=prior a.n and b.n=prior b.n+1;

你可能感兴趣的:(SQL-路漫漫,sql,path,oracle,测试)