2
DELETE FROM `takes` WHERE ID, course_ID IN
(SELECT ID, course_ID FROM `takes` natural join `student`
where name = 'Jane' and course_ID='BIOL101')

From the takes table, I am trying to drop all Janes who are enrolled in BIOL101.

The SELECT gives me the correct set of IDs and course_IDs, and I am just trying to DELETE that from the takes table. Having trouble figuring out the proper syntax.

phpMyAdmin gives me the error: "#1093 - You can't specify target table 'takes' for update in FROM clause"

senjougahara
  • 287
  • 1
  • 4
  • 13
  • 1
    `WHERE ID, course_ID IN (..)` doesn't that look *wrong*? – user2864740 May 24 '14 at 07:47
  • Anyway, see http://stackoverflow.com/a/4192849/2864740 , http://stackoverflow.com/a/2763245/2864740 , and http://dev.mysql.com/doc/refman/5.7/en/delete.html for "DELETE .. JOIN" syntax - boo to using IN for join logic! – user2864740 May 24 '14 at 07:49

3 Answers3

1

Consider not [ab]using IN, but rather use a proper DELETE..JOIN which MySQL does support.

DELETE takes
FROM takes
JOIN student s
  -- The relation/schema is unclear; adapt as required
  -- but MAKE SURE A RELATION IS ESTABLISHED!
  ON s.student_id = takes.student_id
WHERE s.name = 'Jane'
  AND takes.course_ID = 'BIOL101'

Also see this answer.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
0

Try this

  DELETE FROM `takes` WHERE (ID, course_ID) IN
  (select * FROM(
 (SELECT ID, course_ID FROM `takes` natural join `student`
  where name = 'Jane' and course_ID='BIOL101')t )
echo_Me
  • 37,078
  • 5
  • 58
  • 78
0
DELETE
FROM `takes` 
WHERE `ID` IN 
(
    SELECT ID FROM `takes`
    NATURAL JOIN `student`
    WHERE `name` = 'Jane` AND `course_ID` = 'BOIL101'
)
Jacob Cohen
  • 1,232
  • 3
  • 13
  • 33