Person表ID Name Initial
1 Tom  T
2 张三  Z
3 在          Z
4 天天  T如何在用SQL得到这样的JSON数据:{
T:[{Tom},{天天}],
Z:[{张三},{在}]
}谢谢!

解决方案 »

  1.   

    你的意思是用SQL语句进行查询,然会的结果是一个类似JSON格式的字符串么??
      

  2.   


    在数据库里面做效率低。。都是在程序里面实现的。看看这里 
    http://www.cnblogs.com/jyshis/archive/2011/09/07/2169452.html
      

  3.   


    需要看lz的 开发程序使用的语言了。 基本上现在的主流语言都支持直接转json数据格式。 
    .net java 都ok的 。 参考 http://json.codeplex.com
      

  4.   

    之前好像做过一个,sql实现的,不过数据量大了性能可能不会太好
      

  5.   

     我需要SQL语句 JSON转化 用程序语言
      

  6.   

    数据库时sqlite 程序语言时OC 不知道如何用SQL查询出数据 OC可以转化JSON数据的
      

  7.   

    网上找个通用的函数,datatalbe或者dataset转json的函数。我也问问。如果要得到:
    Initial    name1      name2
    ---------- ---------- ----------
    T          Tom        天天
    Z          张三       在或者Initial    nameall
    ---------- ---------- 
    T          Tom,天天
    Z          张三,在该怎么做呢?
      

  8.   

    with cte as
    (
    select cast('{'+a.name+'}' as varchar(100)) as name,a.initial,a.id from #person a where 
    not exists(select 1 from #person x where x.initial=a.initial and x.id<a.id)
    union all
    select cast(b.name+',{'+a.name+'}' as varchar(100)),a.initial,a.id from #person a
    inner join cte b on a.initial=b.initial and a.id>b.id
    where not exists(select 1 from #person x where x.initial=a.initial and x.id>b.id and x.id<a.id)
    )
    select a.initial+':['+a.name+']',a.id from cte  a where exists(
    select 1 from (select initial,max(id) as id 
    from cte group by initial) x where x.id=a.id )
    此方法应该效率可以
      

  9.   


    create table #person(name varchar(10),initial varchar(20),id int identity(1,1))
    insert into #person(name,initial)
    select 'Tom','T' union all
    select '张三','Z'union all
    select '在','Z'union all
    select '天天','T' union all
    select '年年','T' union all
    select '吃饭','Z'
    select * from #personwith cte as
    (
    select cast('{'+a.name+'}' as varchar(100)) as name,a.initial,a.id from #person a where 
    not exists(select 1 from #person x where x.initial=a.initial and x.id<a.id)
    union all
    select cast(b.name+',{'+a.name+'}' as varchar(100)),a.initial,a.id from #person a
    inner join cte b on a.initial=b.initial and a.id>b.id
    where not exists(select 1 from #person x where x.initial=a.initial and x.id>b.id and x.id<a.id)
    )
    select a.initial+':['+a.name+']',a.id from cte  a where exists(
    select 1 from (select initial,max(id) as id 
    from cte group by initial) x where x.id=a.id )