MS-SQL Server 2000 下面是设计的两个表:
用户详细信息表
类型表
现在我想根据用户详细信息表的UserID字段提取出全部的信息,然后让姓名和性别项提前,有没有什么好办法?
就是我查询UserID=1结果是就是把除了姓名性别之外的内容放入详细信息一列或者几列。SQL

解决方案 »

  1.   

    http://bbs.csdn.net/topics/390508677 看看这贴谁能帮助结下贴
      

  2.   

    只能说,表不是我自己设计的╮(╯▽╰)╭ 不过这样能体现很大的灵活性……喔,我也是一个小白,刚刚冒失的发表了下,请多多见谅 
    BOSS这么给的设计方案只能这么搞啊。
      

  3.   

    你还用的是SQL SERVER 2000?公司不打算升级了吗?
      

  4.   

    with a1 (userid,type,content) as
    (
    select 1,1,'甲1' union all
    select 1,2,'男' union all
    select 1,3,'1234567890' union all
    select 1,4,'長安路' union all
    select 2,1,'甲2' union all
    select 2,2,'女' union all
    select 2,3,'0123456789'
    )
    ,a2 (typeid,conName) as
    (
    select 1,'姓名' union all
    select 2,'性別' union all
    select 3,'電話' union all
    select 4,'住址'
    )
    ,a3 as
    (
    select a.*,b.conName
    from a1 a
    inner join a2 b on a.type=b.typeid
    )select a.userid,a.content 姓名,b.content 性別,c.content 祥細信息
    from
    (
    select userid,content
    from a3
    where type=1
    ) a 
    inner join
    (
    select userid,content
    from a3
    where type=2
    ) b on a.userid=b.userid
    left join
    (
    SELECT a.userid,content=stuff
    (
    (select ', '+RTRIM(conName)+':'+RTRIM(content) as [text()]
    from a3 b where a.userid=b.userid and b.type>=3
    for xml path('')
    ),1,2,'') FROM a3 a group by a.userid
    )c  on a.userid=c.userid
      

  5.   

    是啊 2000的。with附近有语法错误 xml附近有语法错误。
      

  6.   

    select userid, max(case conname when conname then [content] else '' end) conname from (select distinct conname  from T_Type) as a
    from T_UserDetail a LEFT JOIN T_Type b ON a.[type]=b.CIndex
    WHERE a.userid=1 GROUP BY [userid]
    我这样写的,(小白勿喷),提示from附近有错误,你能帮我看看那里有错误么
      

  7.   

    select userid, max(case conname when conname then [content] else '' end) conname from (select distinct conname  from T_Type) as a
    from T_UserDetail a LEFT JOIN T_Type b ON a.[type]=b.CIndex
    WHERE a.userid=1 GROUP BY [userid]
    我这样写的,(小白勿喷),提示from附近有错误,你能帮我看看那里有错误么动态拼接,看上面sql server 2000的例子。