选择n行并返回一行

时间:2013-12-07 08:15:24

标签: sql sqlite select join

有这样的sql表:


id          day            name_id_fk
1            1                  1
2            2                  1
3            4                  1
4            3                  1
我想选择这样一行:

day
1|2|4|3

2 个答案:

答案 0 :(得分:1)

select group_concat(day,'|')
from your_table

SQLFiddle demo

答案 1 :(得分:-2)

试试这个Sql ......

DECLARE @cols AS NVARCHAR(MAX)
SET @cols=STUFF((SELECT distinct '|' + CAST( yourcolumnname  AS VARCHAR)
              from  
              (
                SELECT yourcolumnname FROM yourtablename
              ) a
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')        
SELECT @cols

或者你可以试试这个......

DECLARE @cols AS NVARCHAR(MAX)
SET @cols=''
SELECT @cols=@cols+ cast (yourcolumnname AS VARCHAR)+'|' FROM yourtablename
SELECT  substring(@cols,0,len(@cols))