数据有若干记录,其中主键列为:
1
2
3
4
5
.
.
.
我要按照一定的格式取出来,如下的
1,2
3,4
5,6
7,8
.
.
.
要求,需要纯用Select式的SQL实现,不用存储过程和函数等,如何?

解决方案 »

  1.   

    select * from .. where ID=1 or ID=2
    不用函数,那要多少select语句?
      

  2.   

    select id from .. where (id+1)/2=0 union select id from .. where id/2=0 
      

  3.   

    select id from + 表名+where....
      

  4.   

    select id=case
                   when (id&1)!=0 then cast(id as varchar(2))+','+cast((id+1) as varchar(2)) --仅仅考虑条数是成对出现
    end
    from table1 where (id&1)!=0
      

  5.   

    MySQL/Oracle: select concat(a.id+","+b.id) from tablename as a,tablename as b
    where
    (a.id/2)=1 and (b.id/2)=0MS SQL
    concat(a.id+","+b.id)---》a.id+","+b.id
    手头没有数据库,没有进行测试,但是这种自联的方式应该可以。
      

  6.   

    很讨厌这种问 SQL 又不说明用的是什么数据库的问题!
      

  7.   

    我用的oracle数据库,在某个表中的记录不都是一行一行的么,我想让每两行的ID列拼加在一起,例如每行的id数值为a b c d e...那么我取的数就是用逗号分隔加起来,a,b    c,d    e,f  .......
      

  8.   

    还有啊,当最后的时候,有可能会出现一个ID的情况呢前面都是id,id,那么最后一个就是id啦
      

  9.   

    仔细分析下没啥。select a.id, b.id
    from
    (select id, id/2 as row
    from test
    group by id
    having (id & 1)=1)a,
    (select id ,(id-1)/2 as row
    from test
    group by id
    having (id & 1)=0)b
    where a.row=b.row找这个思路自己去扩展吧。
      

  10.   

    select TR.T , TT.T from
    (
    SELECT T, T AS T1
    FROM Ta where T%2=1 ) TR INNER JOIN(SELECT T, (T - 1) AS T1
     FROM Ta where T%2=0) TT ON TR.T1 = TT.T1