一张表relation ,有字段owner,con_id,里面已经有好多记录,owner与con_id是一个唯一键,求写一个UPDATE语句,
把owner='40'改成owner='35' (即要考虑避免与owner-con_id这个唯一键冲突)
owner  con_id
40     12
40     13
35     12
最后形成
owner   con_id
40       12
35       13
35       13     

解决方案 »

  1.   

    owner con_id
    35 13
    35 13
    不是重复了?
      

  2.   

    搞错 ,最后是35 12 ,也就是说如果35这个用户的con_id已经存在,则不能UPDATE OWNER 
      

  3.   

    UPDATE IGNORE ttj SET `OWNER`=35 WHERE `OWNER`=40
      

  4.   

    类似
    update relation as r set  r.owner ='35'
    where r.owner='40' and not exists(select 1 from relation as t2 where t2.con_id =r.con_id and t2.owner='35')
    但是这个语句执行不过去
      

  5.   

    Update relation a inner join (select con_id from relation where owner='35') b on a. con_id=b. con_id
    Set owner ='35'
    Where owner='40'
      

  6.   

    owner 和 con_id 是唯一键,也就是说如果 某人与一个人有了关系,就不能再插入同样的关系,所以楼上有点误解,即owner=35,con_id=12存在,owner=40,con_id=12这条记录就不能变owner,否则会报重复键错误
      

  7.   

    update relation a 
    left join (select con_id from relation where owner = 35)b on b.con_id = a.con_id 
    set a.owner=35 
    where b.con_id is null;这个试试
      

  8.   

    粘贴错了,应该是这样
    update relation a 
    left join (select con_id from relation where owner = 35)b on b.con_id = a.con_id 
    set a.owner=35 
    where b.con_id is null and a.owner = 40;