情景如下:宿舍里有4人间,也有6人间,但不是每个寝室都住满了人,每个床位都有一个编号(不管住没住人),住了人就对应一个学生的学号,没住人学号那一栏就为空。现在要 查询房间只住2个人的房间有哪些,而且那些房间住了哪些人,哪些床位空着???怎么用sql语句查询????

解决方案 »

  1.   

    宿舍表:
    编号 床位数床位表:
    编号 宿舍 学号只住两人的房间:
    select 宿舍  from 床位表 where 学号 is not null group by 宿舍 having count(1)=2空床位:select * from 床位表 where 学号 is null
      

  2.   

    额……其实这张表很简单
    好吧,我还是把你说的表结构贴出来吧
    create table chuangwei (
    cwcode varchar primary key,
    xh varchar,
      

  3.   


    --只住两人的房间:
    select substr(cwcode,1,5) 宿舍 from chuangwei where xh is not null group by substr(cwcode,1,5) having count(1)=2;--空床位:
    select * from chuangwei where xh is null;