0

In my system there are two tables called ‘faculty’ and ‘faculty_assets’.

I need to delete faculty by faculty from the ‘faculty’ table. But system should allow deleting the faculty when only faculty_assets table empty. Otherwise It should not allows to delete the faculties from ‘faculty’ table.

Can anyone modify below sql query? (‘fac_id’ is the primary key of ‘faculty’ table )

function removeFaculty($fac_id){
    $conn=new connection();
    $sql="delete from faculty where fac_id='$fac_id'";
    $result=$conn->query($sql);
    return $result;
}

2 Answers 2

2

You can do this with a not exists clause:

delete from faculty
    where fac_id = '$fac_id' and
          not exists (select 1 from faculty_assets);
Sign up to request clarification or add additional context in comments.

Comments

0

Try to do so:

DELETE t1 FROM faculty t1 LEFT JOIN faculty_assets t2 ON t1.fac_id=t2.fac_id  WHERE t2.fac_id IS NULL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.