请各位高手帮忙:
     假如我有两张及字段如下:
      a  topic_id ,topic_name ....
      b  post_id ,topic_id....
  我的sql语句如下:
   sql=delete from a topic_id not in(select * from b) where topic_id=1;
这样对吗?我的意思是:删除a表,如果a表的id在b表中存在,就不删除,请各位高手指点!

解决方案 »

  1.   

    delete from a where topic_id not in (select topic_id from b);
      

  2.   

    delete from a where topid_id not in (select topic_id from b)
      

  3.   

    或者如下也可以,速度上会有些差别。
    delte a from a left join b using(topic_id) where b.topic_id is null
      

  4.   

    或者
    delete from a where not exists (select 1 from b where topic_id=a.topic_id);
      

  5.   

    delete from a  where topic_id not in(select topic_id from b)  and topic_id=1;
      

  6.   

    delete A from a LEFT JOIN B ON A.topic_id=B.topic_id 
     where A.topic_id=1 AND b.topic_id is null
      

  7.   

    delete from a where topic_id not in (select topic_id from b);
    同意一楼!
      

  8.   

    用这个好点,exists的效率是最高的。
      

  9.   


    delete from a where topic_id not in (select topic_id from b);这个正解~~~~