• delete from inner join


    Update 


    Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,这个在表间关联来做更新和删除操作非常有用. 

    列子: 




    Sql代码 
    update tb_User   
    set pass=''  
    from tb_User usr   
    inner join tb_Address addr on usr.nAddressFK = addr.nAddressID   
    where usr.id=123  

    update tb_User 
    set pass='' 
    from tb_User usr 
    inner join tb_Address addr on usr.nAddressFK = addr.nAddressID 
    where usr.id=123update的格式是 

    update t1 set t1.name=’Liu’ from t1 inner join t2 on t1.id = t2.tid 




    MYSQL,ACCESS 写法如下: 




    Sql代码 
    UPDATE mem_world AS mw1 INNER JOIN mem_world  AS  mw2     
    ON  mw1.parentid = mw2.wid    
    SET mw1.level = mw2.level     
    WHERE mw2.baseid = 107     
    AND  mw2.parentid = 0     
    AND  mw2.size > 1;  

    UPDATE mem_world AS mw1 INNER JOIN mem_world  AS  mw2  
    ON  mw1.parentid = mw2.wid 
    SET mw1.level = mw2.level  
    WHERE mw2.baseid = 107  
    AND  mw2.parentid = 0  
    AND  mw2.size > 1; 

    on是表连接的筛选条件 
    就是说,表连接后,会产生一个类似于临时的视图这么一个东西 
    where是从这个临时的视图中筛选数据的 
    所以,你首先要搞清,你的所谓的2个条件属于哪一种 




    Delete 




    delete 语句也是类似 

    delete from t1 from t1 inner join t2 on t1.id = t2.tid 

    注意蓝色部分。 

    mysql: 


    Sql代码 
    DELETE mwb FROM  mem_world_building AS mwb INNER JOIN mem_world AS mw   
    ON mwb.wid = mw.wid   
    where mw.type between 11 and 15    
    and baseid = 107    
    and mw.parentid <> 0    
    and  mw.size > 1;  

    DELETE mwb FROM  mem_world_building AS mwb INNER JOIN mem_world AS mw 
    ON mwb.wid = mw.wid 
    where mw.type between 11 and 15 
    and baseid = 107 
    and mw.parentid <> 0 
    and  mw.size > 1;  
    下面是ORACLE的: 


    Sql代码 
    DELETE TABLE1 where exists ( select 1 from table2 where and table1.khid=table2.khid and FWDWID=8);  

    DELETE TABLE1 where exists ( select 1 from table2 where and table1.khid=table2.khid and FWDWID=8); 



    Sql代码 
    DELETE TABLE1 where KHID exists ( select KHID from table2 where FWDWID=8)  

    DELETE TABLE1 where KHID exists ( select KHID from table2 where FWDWID=8)

  • 相关阅读:
    记录一次对接XX支付SDK过程中报错问题
    接口回调(重点是理解)
    jQuery form插件的使用--ajaxForm()和ajaxSubmit()的可选参数项对象
    如何通过submit提交form表单获取后台传来的返回值
    Unsupported major.minor version 52.0
    js获取input file路径改变图像地址
    html 横线的代码
    FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
    UUID不失精度,长度改进
    java Date时间的各种转换方式和Mysql存时间类型字段的分析
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8316604.html
Copyright © 2020-2023  润新知