如:
表 a:
选课课号   容量
xkkh       rongliang
1          100
表b(学生选课表):
选课课号   学号
xkkh       xh
1          001
1          002
现在我想要查询:
选课课号  容量   余量
其中 余量=表a中某个选课课号相对应的容量-表b中的选择该门课程的学生总量
请问一条sql如何实现啊?
请各位大虾们帮个忙

解决方案 »

  1.   

    SELECT *
    ,rongliang-(SELECT COUNT(*) FROM B WHERE A.xkkh=B.xkkh)  AS 余量
    FROM A 
      

  2.   


    --试试
    select xkkh as 选课课号,
    rongliang as 容量,
    (rongliang-(select count(1) from b where xkkh=a.xkkh)) as 余量 
    from a
      

  3.   

    SELECT *,
     余量=rongliang-(SELECT COUNT(*) FROM B WHERE A.xkkh=B.xkkh)
    FROM A 
      

  4.   


    create table a(
    xkkh int,
        rongliang int
    )
    goinsert into a(xkkh,rongliang)
    select 1,100
    gocreate table b(
    xkkh int,
        xh varchar(10)
    )
    goinsert into b(xkkh,xh)
    select 1,'001'
    union all select 1,'002'
    goselect xkkh as 选课课号,
    rongliang as 容量,
    (rongliang-(select count(1) from b where xkkh=a.xkkh)) as 余量 
    from a
    godrop table a,b
    go/*
    选课课号  容量   余量
    ---------------------
    1 100 98
      

  5.   

    select xkkh as 选课课号,
    rongliang as 容量,
    (rongliang-(select count(*) from b where xkkh=a.xkkh)) as 余量 
    from a