Search code examples
mysqlmysql-error-1064

MYSQL - Syntax error in Delete query which has in clause


I have a sample schema and its SQL Fiddle is as follows:

http://sqlfiddle.com/#!2/6816b/2

This fiddle just queries the sample database based on condition in where clause as below:

SELECT *
FROM person p_outer
WHERE age IN
(SELECT age AS age1
FROM person p_inner
WHERE first_name='Anne');   
The output of the same is:

    
     ID    FIRST_NAME AGE    GENDER
      1    Bob        25       MM
      8    Anne       25       FF
    
    

I have a delete query which has a inner query as below:

DELETE
FROM person p_outer
WHERE age IN
(SELECT age
FROM person p_inner
WHERE first_name='Anne');

SQL Fiddle for the above query is: http://sqlfiddle.com/#!2/6816b/3

This query works without issue in Oracle DB, when I run the same in MYSQL DB I get the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 
    'p_outer
   WHERE age IN
     (SELECT age
     FROM person p_inner
     WHERE first_n' at line 2: 

Also the MYSQL version is 5.6.13.

Please let know if some issue in the above syntax.


Solution

  • Use this syntax:

    DELETE a.*
    FROM person a
    join person b on a.age = b.age and b.first_name = 'Anne';
    

    fiddle