我现在的语句是这样
INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and  gg<'9' and gg<>'1'我想再加一个条件,就是macau_data表的zd字段要大于data_dttj表zd字段才插入,我就这样写,可是错了
INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and  gg<'9' and gg<>'1' and a.zd>b.zd请问应该怎样写?帮我看看。谢谢!

解决方案 »

  1.   

    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id and a.zd>b.zd) and  gg<'9' and gg<>'1'
      

  2.   

    这样的话是在not in 中,就是说除了这些就插入,但是我是要的才插入,是不是应该写在where后面还是怎样?应该怎么写呢?
      

  3.   

    这样试试:
    INSERT INTO data_dttj SELECT * FROM macau_data c 
    WHERE c.id not in(select a.id from macau_data a, data_dttj b where a.id=b.id ) 
    and c.gg<'9' and c.gg<>'1'
    and c.zd > (select max(zd) as zd from data_dttj)  /*新条件*/
      

  4.   

    这样试试:
    INSERT INTO data_dttj SELECT * FROM macau_data c 
    WHERE c.id not in(select a.id from macau_data a, data_dttj b where a.id=b.id ) 
    and c.gg<'9' and c.gg<>'1'
    and c.zd > (select max(zd) as zd from data_dttj) 这样不对,符合这个条件的插入不了数据
      

  5.   

    这样不对,符合这个条件的插入不了数据
    -----------------------------------------------------------------------------------
    语法应该没有问题.
    "c.zd > (select max(zd) as zd from data_dttj) "这个条件是指macau_data表中zd列大于data_dttj表中所有zd列的行.如果是这个条件导致了插入的数据不符合要求,请楼主指明一下主题中
    "我想再加一个条件,就是macau_data表的zd字段要大于data_dttj表zd字段才插入"
    这段话的确切含义.
    更好的办法是请楼主列些这二个表的测试数据,然后写出希望的结果.
      

  6.   

    我想再加一个条件,就是macau_data表的zd字段要大于data_dttj表zd字段才插入比如
    macau_data表
    id zd
    01  2
    02  1.8
    03  1.5data_dttj表
    id zd
    01 1.9
    02 1.8
    03 1.6这个时候macau_data表只能插入 01 2.0 这行数据到 data_dttj  
    就是要这个效果我现在的语句是这样
    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and  gg<'9' and gg<>'1'应该要改成怎样?谢谢!
      

  7.   

    上面例子
    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id and a.zd<=b.zd
    ) and  gg<'9' and gg<>'1'
      

  8.   

    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id and a.zd<=b.zd
    ) and  gg<'9' and gg<>'1'
    这样的话就会报错:
    由于将在索引、 主关键字、或关系中创建重复的值,请求对表的改变没有成功。 改变该字段中的或包含重复数据的字段中的数据,删除索引或重新定义索引以允许重复的值并再试一次。我用not in就是为了防止这个错误的,请问怎么办呢?
      

  9.   

    不好意思,玩笑过头了我理解楼主可能需要更新数据(两个语句一起执行)update b
     set zd=a.zd,col1=a.col1,.....
    FROM macau_data a, data_dttj b where a.id=b.id and a.zd>b.zd
    and a.gg<'9' and a.gg<>'1'INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and  gg<'9' and gg<>'1'
      

  10.   

    不是吧,又想满足 a.zd>b.zd  又想满足 不重复都不行?
    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and  gg<'9' and gg<>'1'
    这样就是没有重复的,可是我现在想加个判断 a.zd>b.zd  可以写在 最后面吗?还是要怎么改?请指教
      

  11.   

    ......
    怎么感觉lz是个MM?还真做不到一个语句,写个存储过程吧
      

  12.   

    又想满足 a.zd>b.zd  又想满足 不重复都不行?
    -----------------------------------------------------------------------------------
    如果是这样,macau_data表的01 2.0这行怎么能符合条件呢?
    不太明白楼主的需求.
    既然data_dttj表的id不允许重复,那么macau_data表中任何与data_dttj的id同名的行是无法插入到data_dttj表中了.请楼主最好把表之间的业务逻辑展示一下,好让大家对你的业务需求有个清晰的认识,然后帮你出出主意,比这样没效率的猜要好.
      

  13.   

    那两句是先update 还是先 insert呢?如果把不符合我条件的插入进来,更新也没用阿
      

  14.   

    INSERT INTO data_dttj
    select a.id,a.zd
    from macau_data a , data_dttj b
    where a.id=b.id and a.zd>b.zd你看看这个行不行
      

  15.   

    先update如果把不符合我条件的插入进来,更新也没用阿
    -------
    不可能的,你插入的都是原来没有的id,有个not in把关
      

  16.   

    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and  gg<'9' and gg<>'1'那用这个加上去怎么改呢?你这个没有我上面的条件
      

  17.   

    冒牌今天遇到的都是让人糊涂的,比如
    http://community.csdn.net/Expert/topic/4995/4995116.xml?temp=.1583521
    更晕冒牌今天脾气还是很好的,佩服自己一下
      

  18.   

    SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) 
    不知道和
    select * from macau_data c where c.id not in(select id from dttj)
    有什么区别啊
      

  19.   

    不好意思上面错了 应该最后的是 from dttj_data
      

  20.   

    现在我是这样,
    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and gg<'9' and gg<>'1'
    如果 要我想再加一个条件,就是macau_data表的zd字段要大于data_dttj表zd字段才插入比如
    macau_data表
    id zd
    01  2
    02  1.8
    03  1.5data_dttj表
    id zd
    01 1.9
    02 1.8
    03 1.6这个时候macau_data表只能插入 01 2 这行数据到 data_dttj  
    就是要这个效果,请问在我那句基础上加这个效果应该怎么改?
      

  21.   

    这个时候macau_data表只能插入 01 2 这行数据到 data_dttj  
    ----------------------------------------------------------------------------------
    可是data_dttj表中已经有id = '01'了,把01 2这行插入到data_dttj表中会导致id值重复的错误.假设不考虑id值重复的错误,楼主希望插入后data_dttj表的效果是这样的吗?:
    data_dttj表
    id zd
    01 1.9
    02 1.8
    03 1.6
    01 2        /*新插入的行*/
      

  22.   

    说说需要插入后的data_dttj表数据结果
      

  23.   

    INSERT INTO data_dttj SELECT * FROM macau_data c 
    WHERE c.id not in(select a.id from macau_data a, data_dttj b where a.id=b.id ) 
    and c.gg<'9' and c.gg<>'1'
    and c.zd > (select max(zd) as zd from data_dttj) 这样就不报错,但是插入不了我要的效果,是哪里错了?
      

  24.   

    现在我是这样,
    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and gg<'9' and gg<>'1'
    如果 要我想再加一个条件,就是macau_data表的zd字段要大于data_dttj表zd字段才插入比如
    macau_data表
    id zd
    01  2
    02  1.8
    03  1.5data_dttj表
    id zd
    01 1.9
    02 1.8
    03 1.6结果
    data_dttj表
    id zd
    01 2
    02 1.8
    03 1.6就是要这个效果,请问在我那句基础上加这个效果应该怎么改?
      

  25.   

    引用 rea1gz(冒牌realgz V0.3):
    INSERT INTO data_dttj SELECT * FROM macau_data c where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id and a.zd<=b.zd
    ) and  gg<'9' and gg<>'1'
    -----------------------
    这么写报不报错先不说.按照楼主的逻辑,起码也得把最后一个and换成or吧...
      

  26.   

    早就让楼主把要实现的结果写出来,白猜了那么长时间.^^
    就象rea1gz(冒牌realgz V0.3)朋友说的那样,要使用二个SQL才能实现楼主的要求,因为有替换同名id的过程.
    这样试试:
    declare @macau_data table(id varchar(10),zd decimal(10,2))
    insert @macau_data
    select '01',2 union all
    select '02',1.8 union all
    select '03',1.5declare @data_dttj table(id varchar(10),zd decimal(10,2))
    insert @data_dttj
    select '01',1.9 union all
    select '02',1.8 union all
    select '03',1.6----插入符合条件的id不重复行
    INSERT INTO @data_dttj SELECT * FROM @macau_data c 
    WHERE c.id not in(select a.id from @macau_data a, @data_dttj b where a.id=b.id ) 
    --and c.gg<'9' and c.gg<>'1'      /*为了测试,屏蔽了该条件*/
    and c.zd > (select max(zd) as zd from @data_dttj) 
    ----更新同名id的zd值
    update a set zd = b.zd from @data_dttj a,@macau_data b where a.id = b.id and a.zd < b.zd
    --and c.gg<'9' and c.gg<>'1'      /*为了测试,屏蔽了该条件*/----查看
    select * from @data_dttj
      

  27.   

    update a set zd = b.zd from @data_dttj a,@macau_data b where a.id = b.id and a.zd < b.zd这句报错,说b.zd 操作符丢失
      

  28.   

    今天很郁闷。INSERT INTO data_dttj SELECT c.* FROM macau_data c , data_dttj d where c.id not in(SELECT a.id FROM macau_data a, data_dttj b where a.id=b.id ) and  gg<'9' and gg<>'1' and c.zd>d.zd
      

  29.   

    在我的机器上没问题,我的环境是:
    Windows Server2003 + sp1
    SQLSERVER2000 + sp4结果是:
    id    zd
    ---------------------------------   
    01    2.00
    02    1.80
    03    1.60
      

  30.   

    update a set zd = b.zd,kd = b.kd from data_dttj a,macau_data b where a.id = b.id and ((a.zd < b.zd) or (a.kd < b.kd))这样报错:语法错误 (操作符丢失) 在查询表达式 'b.kd from data_dttj a' 中。请问怎么办?
      

  31.   

    update a set zd = b.zd,kd = b.kd from data_dttj a,macau_data b where a.id = b.id and ((a.zd < b.zd) or (a.kd < b.kd))这样报错:语法错误 (操作符丢失) 在查询表达式 'b.kd from data_dttj a' 中。请问怎么办?
      

  32.   

    上面的朋友,说下这个update语句吧,我现在用两句,但是还是这样
      

  33.   

    是的,
    update a set zd = b.zd,kd = b.kd from data_dttj a,macau_data b where a.id = b.id and ((a.zd < b.zd) or (a.kd < b.kd))这样报错:语法错误 (操作符丢失) 在查询表达式 'b.kd from data_dttj a' 中。
      

  34.   

    麻烦查看帖子的朋友,帮忙把下面的代码在您的机器上试一下,看看是否会出现楼主所说的操作符丢失错误:
    declare @macau_data table(id varchar(10),zd decimal(10,2))
    insert @macau_data
    select '01',2 union all
    select '02',1.8 union all
    select '03',1.5declare @data_dttj table(id varchar(10),zd decimal(10,2))
    insert @data_dttj
    select '01',1.9 union all
    select '02',1.8 union all
    select '03',1.6----插入符合条件的id不重复行
    INSERT INTO @data_dttj SELECT * FROM @macau_data c 
    WHERE c.id not in(select a.id from @macau_data a, @data_dttj b where a.id=b.id ) 
    --and c.gg<'9' and c.gg<>'1'      /*为了测试,屏蔽了该条件*/
    and c.zd > (select max(zd) as zd from @data_dttj) 
    ----更新同名id的zd值
    update a set zd = b.zd from @data_dttj a,@macau_data b where a.id = b.id and a.zd < b.zd
    --and c.gg<'9' and c.gg<>'1'      /*为了测试,屏蔽了该条件*/----查看
    select * from @data_dttj
      

  35.   

    hellowork(一两清风) 这位哥哥,你试试用
    update a set zd = b.zd,kd = b.kd from data_dttj a,macau_data b where a.id = b.id and ((a.zd < b.zd) or (a.kd < b.kd))
    这句看看会不会报错
      

  36.   

    忘记说了,我这个是access数据库,不过也是用sql语句。和sql server应该一样的吧,但是就是不明白这句为什么错了
      

  37.   

    这点儿委屈算什么,你可能还没遇到过被开发工具的某些BUG搞得焦头烂额痛不欲生的时候.忘记说了,我这个是access数据库,不过也是用sql语句。和sql server应该一样的吧,但是就是不明白这句为什么错了
    ------------------------------------------------------------------------------------
    从你提出那个操作符丢失错误开始,我就怀疑楼主用的是不是SQLSERVER.因为我用SQLSERVER时从来没遇到这种错误.可以肯定是楼主的代码与ACCESS的JET-SQL语法不兼容.请楼主参考一下ACCESS的语法吧.我不熟悉ACCESS.
      

  38.   

    但是我看access和sql server 基本都差不多哦
      

  39.   

    没用过ACCESS,但可以肯定ACCESS的语法与T-SQL有不同的地方,请楼主仔细参读一下吧.
    所谓的基本差不多,是指都符合SQL ANSI-92标准,在SELECT,UPDATE,DELETE时语法基本相同,但各种DBMS系统会在这些相同的基础上进行扩展演化的.
      

  40.   

    不用这么麻烦``版主用iner join试试
      

  41.   

    INSERT INTO data_dttj SELECT * FROM macau_data c where exists(SELECT a.id FROM macau_data a, data_dttj b where a.id<>b.id and a.zd>b.zd) and  gg<'9' and gg<>'1'
      

  42.   

    那个gg是什么呀?你的表里没看到有这个字段呀?
    还有
    update a set zd = b.zd,kd = b.kd from data_dttj a,macau_data b where a.id = b.id and ((a.zd < b.zd) or (a.kd < b.kd))
    这句的语法应该是错误的