表1:   编号   数量    表2; 编号   数量   表3:  编号   数量
         1     2             1    3           1     1
        2     2             2    1           2     3
        3     1             3    1           3     2
查询  当表1.编号.1=表2.编号.1     那么 数量=表2.数量-表1.数量
  结果  :
          编号    数量 
          1        1
         2        -1
        3        0
    查询  当表1.编号.1=表3.编号.1     那么 数量=表3.数量-表1.数量
  结果  :
          编号    数量 
          1        -1
         2        1
         3        1
  怎么写查询语句呢

解决方案 »

  1.   

    select 表2.编号,表2.数量-表1.数量 as 数量
    from 表1 join 表2 on 表1.编号=表2.编号
      

  2.   

    select tb1.*,
           [表2.数量-表1.数量] = isnull((select 数量 from tb2 where tb2.编号 = tb1.编号) ,0) - 数量,
           [表3.数量-表1.数量] = isnull((select 数量 from tb3 where tb3.编号 = tb1.编号) ,0) - 数量
    from tb1
      

  3.   

    create table tb1(编号 int, 数量 int)
    create table tb2(编号 int, 数量 int)
    create table tb3(编号 int, 数量 int)
    insert into tb1 values(1  ,  2)
    insert into tb1 values(2  ,  2)
    insert into tb1 values(3  ,  1)
    insert into tb2 values(1  ,  3)
    insert into tb2 values(2  ,  1)
    insert into tb2 values(3  ,  1)
    insert into tb3 values(1  ,  1)
    insert into tb3 values(2  ,  3)
    insert into tb3 values(3  ,  2)
    goselect tb1.编号,
           [表2.数量-表1.数量] = isnull((select 数量 from tb2 where tb2.编号 = tb1.编号) ,0) - 数量,
           [表3.数量-表1.数量] = isnull((select 数量 from tb3 where tb3.编号 = tb1.编号) ,0) - 数量
    from tb1drop table tb1 , tb2, tb3/*
    编号          表2.数量-表1.数量 表3.数量-表1.数量 
    ----------- ----------- ----------- 
    1           1           -1
    2           -1          1
    3           0           1(所影响的行数为 3 行)*/
      

  4.   

    你把那个查询分开写不就是你要的所谓的两个结果?create table tb1(编号 int, 数量 int)
    create table tb2(编号 int, 数量 int)
    create table tb3(编号 int, 数量 int)
    insert into tb1 values(1  ,  2)
    insert into tb1 values(2  ,  2)
    insert into tb1 values(3  ,  1)
    insert into tb2 values(1  ,  3)
    insert into tb2 values(2  ,  1)
    insert into tb2 values(3  ,  1)
    insert into tb3 values(1  ,  1)
    insert into tb3 values(2  ,  3)
    insert into tb3 values(3  ,  2)
    go--表2.数量-表1.数量
    select tb1.编号,
           [表2.数量-表1.数量] = isnull((select 数量 from tb2 where tb2.编号 = tb1.编号) ,0) - 数量
    from tb1
    /*
    编号          表2.数量-表1.数量 
    ----------- ----------- 
    1           1
    2           -1
    3           0(所影响的行数为 3 行)
    */--表3.数量-表1.数量 
    select tb1.编号,
           [表3.数量-表1.数量] = isnull((select 数量 from tb3 where tb3.编号 = tb1.编号) ,0) - 数量
    from tb1
    /*
    编号          表3.数量-表1.数量 
    ----------- ----------- 
    1           -1
    2           1
    3           1(所影响的行数为 3 行)
    */--合在一起写。
    select tb1.编号,
           [表2.数量-表1.数量] = isnull((select 数量 from tb2 where tb2.编号 = tb1.编号) ,0) - 数量,
           [表3.数量-表1.数量] = isnull((select 数量 from tb3 where tb3.编号 = tb1.编号) ,0) - 数量
    from tb1
    /*
    编号          表2.数量-表1.数量 表3.数量-表1.数量 
    ----------- ----------- ----------- 
    1           1           -1
    2           -1          1
    3           0           1(所影响的行数为 3 行)*/drop table tb1 , tb2, tb3
      

  5.   

    你误会了,我是外面有个接口,能不能当在外面输入查询编号(表2或表3),查询出来是   编号  数量  ,这样的里面的SQL语句是固定的
      

  6.   

    create table tb1(编号 int, 数量 int)
    create table tb2(编号 int, 数量 int)
    create table tb3(编号 int, 数量 int)
    insert into tb1 values(1  ,  2)
    insert into tb1 values(2  ,  2)
    insert into tb1 values(3  ,  1)
    insert into tb2 values(1  ,  3)
    insert into tb2 values(2  ,  1)
    insert into tb2 values(3  ,  1)
    insert into tb3 values(1  ,  1)
    insert into tb3 values(2  ,  3)
    insert into tb3 values(3  ,  2)
    select tb1.编号,tb2.数量-tb1.数量 as  数量 from tb1,tb2 where tb1.编号=tb2.编号
    select tb1.编号,tb3.数量-tb1.数量 as 数量   from tb1,tb3 where tb1.编号=tb3.编号
    drop table tb1,tb2,tb3/*
    编号          数量
    ----------- -----------
    1           1
    2           -1
    3           0(3 行受影响)编号          数量
    ----------- -----------
    1           -1
    2           1
    3           1
    */
      

  7.   

    你如果这么做,表名就是动态的了,的组合SQL语句来动态执行.以下是基本语法,自己去组合起来就行了.
    动态sql语句基本语法 
    1 :普通SQL语句可以用Exec执行 eg: Select * from tableName 
     Exec('select * from tableName') 
     Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg: 
    declare @fname varchar(20) 
    set @fname = 'FiledName' 
    Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。 
    Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可 
    declare @fname varchar(20) 
    set @fname = 'FiledName' --设置字段名 declare @s varchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s) -- 成功 
    exec sp_executesql @s -- 此句会报错 declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s) -- 成功 
    exec sp_executesql @s -- 此句正确 3. 输出参数 
    declare @num int, 
     @sqls nvarchar(4000) 
    set @sqls='select count(*) from tableName' 
    exec(@sqls) 
    --如何将exec执行结果放入变量中? declare @num int, 
     @sqls nvarchar(4000) 
    set @sqls='select @a=count(*) from tableName ' 
    exec sp_executesql @sqls,N'@a int output',@num output 
    select @num 
      

  8.   


    create table tb1(编号 int, 数量 int)
    create table tb2(编号 int, 数量 int)
    create table tb3(编号 int, 数量 int)
    insert into tb1 values(1  ,  2)
    insert into tb1 values(2  ,  2)
    insert into tb1 values(3  ,  1)
    insert into tb2 values(1  ,  3)
    insert into tb2 values(2  ,  1)
    insert into tb2 values(3  ,  1)
    insert into tb3 values(1  ,  1)
    insert into tb3 values(2  ,  3)
    insert into tb3 values(3  ,  2)
    declare @a varchar(20)
    set @a='tb2'
    exec('select a.编号,b.数量-a.数量 as  数量 from tb1 a, '+@a+' b where a.编号=b.编号')set @a='tb3'
    exec('select a.编号,b.数量-a.数量 as  数量 from tb1 a, '+@a+' b where a.编号=b.编号')drop table tb1,tb2,tb3/*
    编号          数量
    ----------- -----------
    1           -1
    2           1
    3           1(3 行受影响)
    */
      

  9.   


    SELECT A.编号,B.数量-A.数量 AS 数量 FROM 表1 AS A INNER JOIN 表2 AS B ON A.编号=B.编号