更新A表里面a1列的值.
update A set a1=3 where
条件是A表mon列的值1与7的B表里面b1字段的值为1的不更新,其它的都更新表结构
b表.
id,positID,b1
1   1      1
1   2      0
A表
id  positID a1 mon
1    1      4  7
1    1      4  6
1    1      4  5
1    1      4  4
1    1      4  3
1    1      4  2
1    1      4  11    2      4  7
1    2      4  6
1    2      4  5
1    2      4  4
1    2      4  3
1    2      4  2
1    2      4  1结果
/*
A表
id  positID a1 mon
1    1      4  7
1    1      3  6
1    1      3  5
1    1      3  4
1    1      3  3
1    1      3  2
1    1      4  11    2      3  7
1    2      3  6
1    2      3  5
1    2      3  4
1    2      3  3
1    2      3  2
1    2      3  1*/

解决方案 »

  1.   

    update a set a1=3 from a,b where a.positID=b.positID and a.mon not in ('5,','7') and  b.b1<>1
      

  2.   

    update a set a1=3 where not exists
    (select * from a where a.positid=1 and mon in(1,7))
    --这样行不?
      

  3.   


    update a set a1=3 from a,b where a.positID=b.positID and a.mon not in ('1,','7') and  b.b1!=1
      

  4.   

    这样的方法结果变成这样了/*
    A表
    id  positID a1 mon
    1    1      4  7
    1    1      3  6
    1    1      3  5
    1    1      3  4
    1    1      3  3
    1    1      3  2
    1    1      4  11    2      4  7
    1    2      3  6
    1    2      3  5
    1    2      3  4
    1    2      3  3
    1    2      3  2
    1    2      4  1*/
      

  5.   


    create table b(id int,positID int,b1 int)
    insert b
    select 1   ,1  ,    1 union all
    select 1   ,2   ,   0create table A(id int,positID int,a1 int,mon int)
    insert a
    select 1 ,   1    ,  4  ,7 union all
    select 1  ,  1   ,   4  ,6 union all
    select 1  ,  1   ,   4  ,5 union all
    select 1  ,  1   ,   4  ,4 union all
    select 1  ,  1    ,  4  ,3 union all
    select 1  ,  1    ,  4  ,2 union all
    select 1  ,  1   ,   4  ,1 union all
    select 1  ,  2    ,  4  ,7 union all
    select 1  ,  2    ,  4  ,6 union all
    select 1  ,  2   ,   4  ,5 union all
    select 1 ,   2   ,   4  ,4 union all
    select 1   , 2   ,   4  ,3 union all
    select 1   , 2   ,   4  ,2 union all
    select 1  ,  2   ,   4  ,1update A set a1=3 
    where not exists
    (select 1 from b where a.positID=b.positID
    and a.mon in(1,7) and  b.positID=1)
    select * from adrop table a,b/*
    id          positID     a1          mon         
    ----------- ----------- ----------- ----------- 
    1           1           4           7
    1           1           3           6
    1           1           3           5
    1           1           3           4
    1           1           3           3
    1           1           3           2
    1           1           4           1
    1           2           3           7
    1           2           3           6
    1           2           3           5
    1           2           3           4
    1           2           3           3
    1           2           3           2
    1           2           3           1*/
      

  6.   

    我的学习一直等到你不少的帮助.太感动了...想问一下select 1 from 这个1是什么?
      

  7.   

    就是select * 的意思
    在MSDN上看到1的含义.我想了解一下为什么有这个1之类的.分不够,加点分,不然你们太热心了.这点分对不住你们的这分热心...
      

  8.   

    1. 1<>1 的用处:
    用于只取结构不取数据的场合,例如:
    create table table_temp tablespace tbs_temp as
    select * from table_ori where 1<>1 
    建成一个与table_ori 结构相同的表table_temp,但是不要table_ori 里的数据。
    (除了表结构,其它结构也同理)2.1=1用于动态SQL,例如:
    lv_string = 'select tbl_name,tbl_desc from tbl_test where 1=1 '+l_condition;
    当用户选择了查询的名称'abc'时
    l_condition ='and tbl_name = ''abc''';
    但是当用户没有选择名称查询时l_condition就为空串''这样 
    lv_string = 'select tbl_name,tbl_desc from tbl_test where 1=1 ',
    运行也不会出错,相当于没有限制名称条件。但是如果没有1=1的条件,则
    lv_string = 'select tbl_name,tbl_desc from tbl_test where ';
    这样就会报错。
      

  9.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-07-10 21:03:22
    -- Verstion:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([id] int,[positID] int,[a1] int,[mon] int)
    insert [a]
    select 1,1,4,7 union all
    select 1,1,4,6 union all
    select 1,1,4,5 union all
    select 1,1,4,4 union all
    select 1,1,4,3 union all
    select 1,1,4,2 union all
    select 1,1,4,1 union all
    select 1,2,4,7 union all
    select 1,2,4,6 union all
    select 1,2,4,5 union all
    select 1,2,4,4 union all
    select 1,2,4,3 union all
    select 1,2,4,2 union all
    select 1,2,4,1
    --> 测试数据:[b]
    if object_id('[b]') is not null drop table [b]
    go 
    create table [b]([id] int,[positID] int,[b1] int)
    insert [b]
    select 1,1,1 union all
    select 1,2,0
    --------------开始查询--------------------------
    update a set a1=3 from a,b where a.positID=b.positID and a.mon not in (5,7) and  b.b1<>1select * from a
    ----------------结果----------------------------
    /* (14 行受影响)(2 行受影响)(5 行受影响)
    id          positID     a1          mon
    ----------- ----------- ----------- -----------
    1           1           4           7
    1           1           4           6
    1           1           4           5
    1           1           4           4
    1           1           4           3
    1           1           4           2
    1           1           4           1
    1           2           4           7
    1           2           3           6
    1           2           4           5
    1           2           3           4
    1           2           3           3
    1           2           3           2
    1           2           3           1(14 行受影响)
    */
      

  10.   

    update t set a1=3 from A t 
    left join B s on t.positID=s.positID and s.positID=1
    where t.mon not in(1,7)
      

  11.   


    --try
    update t set a1=3 from A t 
    left join B s on t.positID=s.positID and s.positID=1
    where t.mon not in(1,7) and s.positID is null
      

  12.   

    谢谢.船长
    我想知道的是豆子那样写select 1 from 我想看一下这方面的资料.在MSDN没有找到.为什么1可以代替*号
    在哪里要可以看到这方面的资料.
    反正问题解决了.只是想加深一下知识.还有看到别人写的select * from table where where type='p' 
    这个P说是存储过程.我也想知道在哪些书上有看到.或网上资源文档.一直想加深这些知识可遗憾总是找不到这类的资料,要不全是一些不占边的资料
      

  13.   

    http://flash7783.iteye.com/blog/691393
      

  14.   

    小F,我的问题用豆子那个方法已经解决了.你的这个在POSTID2里面没有更新到.