zoukankan      html  css  js  c++  java
  • 如何将sql查询出的结果,用符号隔开

    晚饭过后,打开QQ圈子,发现QQ群里有人提问了一个问题↓

    数据表中有这样的数据
    1 100
    1 101 
    1 106
    2 100
    2 109
    3 112
    如何转换为
    1 100,101,106
    2 100,109
    3 112

    知道写存储过程或者函数可以解决,但是想想能不能用一条sql语句解决...未果...

    还是去搜索了下怎么搞,→转载链接←

    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    go
    create function [dbo].[f_str](@id int) returns nvarchar(1000)
    as
    begin
    declare @str nvarchar(1000)
    set @str = ''
    select @str = @str + ',' + cast(value as nvarchar(900)) from tb where id = @id
    set @str = right(@str , len(@str) - 1)
    return @str
    end
    go
    --调用函数
    select id , value = dbo.f_str(id) from tb group by id 
  • 相关阅读:
    swift
    swift
    swift
    swift
    swift
    swift
    swift
    选择排序
    组合 和 继承
    Android中使用LitePal操控SQLite数据库
  • 原文地址:https://www.cnblogs.com/love-zf/p/6480955.html
Copyright © 2011-2022 走看看