0

I'm trying to use the following SELECT statement to craft a DELETE statement after I get it working.

Basically table "listing" has a bunch of record that need to be removed. If the EmpNo cannot be found in address table then I want to remove the record from listing table. I keep getting a invalid syntax. What am I doing wrong?

SELECT A.*
FROM address A
LEFT JOIN listing B
USING (EmpNo)
WHERE B.EmpNo IS <> A.EmpNo
2
  • 1
    In the future, please post the error message. It is pretty easy to spot this time, but isn't always. And MySQL 1064 errors typically point to exactly the place where the error occurs. Commented Feb 26, 2013 at 20:15
  • Okay, will do. This one just gave a really generic error so I assumed I had really messed up the query. Commented Feb 26, 2013 at 20:27

1 Answer 1

2

Remove the IS keyword here:

WHERE B.EmpNo IS <> A.EmpNo

Should be:

WHERE B.EmpNo  <> A.EmpNo

If EmpNo exists with the same name in both tables, USING will work correctly there. Otherwise, you can be a little more explicit with ON:

FROM 
  address A
  LEFT JOIN listing B
    ON A.EmpNo = B.EmpNo

To find those records in A with no match in B, test for B.EmpNo IS NULL instead of B.EmpNo <> A.EmpNo.

WHERE B.EmpNo IS NULL
Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm..I am trying this and it is deleting all records in listing, even the ones where there is a match with address? DELETE A.* FROM address A LEFT JOIN qemplisting B ON B.EmpNo = A.EmpNo
@RoccoTheTaco Sorry I missed this comment. To delete only those not matching, you need WHERE B.EmpNo IS NULL - meaning there's a record in A which has no matching record in B.

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.