多表查询题
现在有四张表分别是:p_Building(楼栋表),p_Room(房间表),p_Customer(客户表),p_Cst2Room(客户房间中间表),表字段如下图:
表名:p_Building
 
Id BldName    
1 四栋    
2 一栋    
3 三栋    
4 二栋  表名:P_Room
 
Id RoomNo RoomArea BldId    
1 101 80 1    
2 102 80 2    
3 103 99 3    
4 104 87 4  
表名:p_Customer
 
Id CstName CstTel CstSex    
1 张三 13921321332 男    
2 李四 13987778784 女    
3 王五 13232321233 男    
4 赵六 13443223323 男  
表名:p_Cst2Room
 
Id RoomId CstId CstNo    
1 1 1 1    
2 1 2 2    
3 1 3 3    
4 1 4 4  通过SQL语句,实现下面查询结果(一):
 
楼栋名称 房号 客户名称 房间面积    
四栋 101 张三;李四;王五;赵六 80  主要思考问题:如何将这间房的所有权益人的名字全部显示?
Select BldNam as
通过SQL语句,实现下面查询结果(二):
 
楼栋名称 房间号码 房间面积    
一栋 102 80    
二栋 104 87    
三栋 103 99    
四栋 101 80  主要思考问题:如何通过中文一二三四五来排序?单表查询题 
学生姓名 课程名称 考试成绩    
张三 英语 88    
张三 数学 87    
李四 数学 90    
李四 英语 92    
王五 英语 79    
王五 数学 90  请查出数学成绩低于英语成绩的学生信息,字段要求显示学生姓名、数学成绩、英语成绩?函数题
根据单表查询题的表,创建一个函数接收输入一个学生姓名,然后输出这个学生成绩最高的课程名?
触发器题
根据单表查询题的表,创建一个触发器,当修改某个学生其中一门课的成绩后,打印出增加多少分,或者减少多少分?

解决方案 »

  1.   


    问题1参考
    --合并列值
    --原著:邹建
    --改编:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)  2007-12-16  广东深圳--表结构,数据如下:
    /*
    id    value 
    ----- ------ 
    1    aa 
    1    bb 
    2    aaa 
    2    bbb 
    2    ccc 
    */
    --需要得到结果:
    /*
    id    values 
    ------ ----------- 
    1      aa,bb 
    2      aaa,bbb,ccc 
    即:group by id, 求value 的和(字符串相加)
    */
    --1. 旧的解决方法(在sql server 2000中只能用函数解决。) 
    --1. 创建处理函数
    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 varchar(8000) 
    as 
    begin 
        declare @r varchar(8000) 
        set @r = '' 
        select @r = @r + ',' + value from tb where id=@id 
        return stuff(@r, 1, 1, '') 
    end 
    go -- 调用函数
    SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id drop table tb 
    drop function dbo.f_str /* 
    id          value      
    ----------- ----------- 
    1          aa,bb 
    2          aaa,bbb,ccc 
    (所影响的行数为2 行)
    */ --2、另外一种函数. 
    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 f_hb(@id int) 
    returns varchar(8000) 
    as 
    begin 
      declare @str varchar(8000) 
      set @str = '' 
      select @str = @str + ',' + cast(value as varchar) from tb where id = @id 
      set @str = right(@str , len(@str) - 1) 
      return(@str) 
    End 
    go --调用自定义函数得到结果:
    select distinct id ,dbo.f_hb(id) as value from tb drop table tb 
    drop function dbo.f_hb /* 
    id          value      
    ----------- ----------- 
    1          aa,bb 
    2          aaa,bbb,ccc 
    (所影响的行数为2 行)
    */ --2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。) 
    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 
    -- 查询处理
    select * from(select distinct id from tb)a outer apply( 
            select [values]= stuff(replace(replace( 
                ( 
                    select value from tb n 
                    where id = a.id 
                    for xml auto 
                ), ' <N value="', ','), '"/>', ''), 1, 1, '') 
    )N 
    drop table tb /* 
    id          values 
    ----------- ----------- 
    1          aa,bb 
    2          aaa,bbb,ccc (2 行受影响) 
    */ --SQL2005中的方法
    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 select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '') 
    from tb 
    group by id /* 
    id          values 
    ----------- -------------------- 
    1          aa,bb 
    2          aaa,bbb,ccc (2 row(s) affected) */ drop table tb
      

  2.   


    --请查出数学成绩低于英语成绩的学生信息,字段要求显示学生姓名、数学成绩、英语成绩?create table tb
    (
     学生姓名 nvarchar(3),
     课程名称 nvarchar(10),
     考试成绩 int
    )insert into tb
    select '张三','英语',88 union all  
    select '张三','数学',87 union all    
    select '李四','数学',90 union all    
    select '李四','英语',92 union all    
    select '王五','英语',79 union all    
    select '王五','数学',90  select t1.学生姓名,t1.考试成绩 AS 数学成绩,t2.考试成绩 AS 英语成绩 from( 
    select * from tb where 课程名称='数学') t1, (select * from tb where 课程名称='英语') t2 where t1.学生姓名=t2.学生姓名 and t1.考试成绩<t2.考试成绩
      

  3.   


    --函数题
    --根据单表查询题的表,创建一个函数接收输入一个学生姓名,然后输出这个学生成绩最高的课程名?create function [dbo].[getKC](@name nvarchar(3))
    returns varchar(1000)
    as
    begin
      declare @kc nvarchar(10)
      
      select top 1 @kc=课程名称 from tb where 学生姓名='' + @name + ''  order by 考试成绩 desc
      return @kc
    ENDselect dbo.[getKC]('张三')
      

  4.   


    --触发器题
    --根据单表查询题的表,创建一个触发器,当修改某个学生其中一门课的成绩后,打印出增加多少分,或者减---少多少分? 
     
     create trigger tr_tb_Update on tb for update
    AS
     begin
       declare @oddScore int
       declare @newScore int
       select @oddScore=考试成绩 from deleted
       select @newScore=考试成绩 from inserted
       if(@oddScore>@newScore)
        begin
          print('减少' + cast((@oddScore - @newScore) as nvarchar) + '分')
        end
        else
          begin
          print('增加' + cast((@newScore - @oddScore) as nvarchar) + '分')
        end
        
     end
     
     
     update tb set 考试成绩=92 where 学生姓名='张三' and  课程名称='英语'
      

  5.   


     ----大写字母排序问题---最好建立一个排序字段,实在不行,用下面的方法create table #t
    (
      name nvarchar(10)
    )insert into #t 
    select '四楼' union all
    select '一楼' union all
    select '二楼' union all
    select '三楼' select * from #t 
    order by charindex(name,'一楼,二楼,三楼,四楼')