有四张表 图片表,教师资料表,视频表  这4张表,以课程表为中心,主键FID   另外三张表中 各有一个字段来关联 课程表  设立外键kcID ,那么我现在删除 课程表的时候,先要检查下另外三个表里(也就是已知课程FID,怎么到三张表里去查询,写一个sql语句) 是否已添加内容,如果其中某张表里添加了内容,那么就不能删除 课程,提示该课程下已存在内容,不能删除。。这个sql语句 该 怎么写呢?谢谢了先

解决方案 »

  1.   

    delete a from 课程表 as a 
    where not exists(select 1 from 图片表 where 图片表ID=a.图片表ID)
    and not exists(select 1 from 教师资料表 where 教师资料表ID=a.教师资料表ID)
    and not exists(select 1 from 视频表 where 视频表ID=a.视频表ID)
      

  2.   


    delete a from 课程表 as a 
    where not exists(select 1 from 图片表 where kcid=a.fid)
    and not exists(select 1 from 教师资料表 where kcid=a.fid)
    and not exists(select 1 from 视频表 where kcid=a.fid)
      

  3.   

    delete from 课程表 where FID not in (select kcid from 图片表 )
    and FID not in (select kcid from 教师资料表)
    and FID not in (select kcid from 视频表)
      

  4.   


    if not exists(select 1 from 图片表 where kcID=1) and not exists(select 1 from 教师资料表 where kcID=1) and not exists(select 1 from 视频表 where kcID=1) 
       delete 课程表 where id=1
      

  5.   


    方法一:
    delete a from 课程表 as a 
    where FID=1
    and not exists(select 1 from 图片表 where kcID=1)
    and not exists(select 1 from 教师资料表 where kcID=1)
    and not exists(select 1 from 视频表 where kcID=1)
    方法二(改一下上面的):
    if not exists(select 1 from 图片表 where kcID=1) and not exists(select 1 from 教师资料表 where kcID=1) and not exists(select 1 from 视频表 where kcID=1) 
       delete 课程表 where FID=1 --这里应该是FID