有表t,表b,其中t有id,title,content,表b有id,tid,title,content.其中t的id与b的tid是关联的.
现在我想删b中content为空的b记录和t中相应的记录?
该怎么写?
有一天

解决方案 »

  1.   

    删b中content为空的b记录和t中相应的记录同时删除吗?
      

  2.   

    delete b where content is null
    go
    delete t where not exists(select 1 from b where tid=t.id)
    go
      

  3.   

    delete b where content is null
    delete t from t inner join b on t.id=b.tid where b.content is null 
      

  4.   

    delete t from t inner join b on t.id=b.tid where b.content is null 
    delete b where content is null
      

  5.   

    试一下delete t ,b 
    from 
    b left join t on b.id = t.id where b.content is null
      

  6.   

    首先的问题是tid是主键还是T表中ID是主键?
    如果要删除两条相应记录要先删除外键表,后主键表。因为外键的值在主键表里一定要有,所以删除要注意先后。
    如果T中id是主键则:
    即:
    delete b where content is null --删除b中记录
    delete t where t.id not in(select tid from b) //这个语句很可能有错误,但是大概思路是这样
    如果tid是主键。
    则:
    delete t from t inner join b on t.id=b.tid where b.content is null 
    delete b where content is null
      

  7.   


    delete t from t inner join b on t.id=b.tid where b.content is null  and len(b.content)=0
    delete b where content is nulland len(b.content)=0
    可以满足,但最好用触发器