zoukankan      html  css  js  c++  java
  • 来自中油瑞飞的SQL笔试题20131202

    1、有三张表,用户表,用户角色表,角色表,
    使用sql显示如下内容:
    用户ID,用户名,超级管理员,录入员,会计
    也就是角色用逗号分隔。
    解:

    1、填充数据到表User
    select * from [User]
    INSERT INTO [northwind].[dbo].[User]
               ([ID]
               ,[NAME])
         VALUES
               (1
               ,'zhaohy')
               INSERT INTO [northwind].[dbo].[User]
               ([ID]
               ,[NAME])
         VALUES
               (2
               ,'zhangyy')
    GO

    2、填充数据到表role
    select * from [role]
    INSERT INTO [northwind].[dbo].[Role]
               ([ID]
               ,[RoleName])
         VALUES
               (1
               ,'senior software engineer')
               INSERT INTO [northwind].[dbo].[Role]
               ([ID]
               ,[RoleName])
         VALUES
               (2
               ,'project manager')
                        INSERT INTO [northwind].[dbo].[Role]
               ([ID]
               ,[RoleName])
         VALUES
               (3
               ,'UI Disigner')
                                   INSERT INTO [northwind].[dbo].[Role]
               ([ID]
               ,[RoleName])
         VALUES
               (4
               ,'Tester')
    GO
    3、填充数据到表Role_User
    select * from Role_User
    INSERT INTO [northwind].[dbo].[Role_User]
               ([RoleID]
               ,[UserID])
         VALUES
               (1
               ,1)
    GO
    INSERT INTO [northwind].[dbo].[Role_User]
               ([RoleID]
               ,[UserID])
         VALUES
               (2
               ,1)
               INSERT INTO [northwind].[dbo].[Role_User]
               ([RoleID]
               ,[UserID])
         VALUES
               (3
               ,1)
                          INSERT INTO [northwind].[dbo].[Role_User]
               ([RoleID]
               ,[UserID])
         VALUES
               (4
               ,2)

    4、查询出来:
    drop table #result;
    select * into #result from (select u.ID,u.NAME,ru.RoleID,r.RoleName from [user] u inner join Role_User ru on ru.UserID =u.ID inner join [Role] r
    on ru.RoleID=r.ID) as t;
    select * from #result;
    select id,name, [RoleName] = stuff((select ',' + [RoleName] from #result t where id = #result.id for xml path('')) , 1 , 1 , '') 
    from #result 
    group by id ,name;
    drop table #result;
    输出结果:
     

  • 相关阅读:
    HTML onblur 事件属性
    插入光标颜色 | caret-color (Basic User Interface) – CSS 中文开发手册
    gc (Runtime) – Python 中文开发手册
    《宾狗》
    《凭什么相信你,我的CNN模型?(篇二:万金油LIME)》
    《凭什么相信你,我的CNN模型?(篇一:CAM和Grad-CAM)》
    《如何利用CAM(类激活图)动态可视化模型的学习过程》
    《Attention最新进展》
    TCP-IP四书五经
    《统计学习方法》
  • 原文地址:https://www.cnblogs.com/riasky/p/3455399.html
Copyright © 2011-2022 走看看