SQL 从子查询中删除多行的语法错误

SQL syntax error for deleting multiple rows from a subquery

我有一个使用 InnoDB 的关系 MySQL 数据库,它将课程与课程参加者联系起来。此数据库的问题在于 course_attendees 中的 course_id 列未设置为外键。 course_attendees table 试图引用的地方缺少很多课程。

我想删除那些记录,因为这些课程不再属于它们了。我写了这个 select 查询,其中 select 是应该删除的所有课程:

SELECT
    ca.`id`
  FROM `course_attendees` AS ca
    LEFT JOIN `courses` c
      ON ca.`course_id` = c.`id`
  WHERE c.`id` IS NULL

现在,当我尝试使用这样的子查询将其包装在 DELETE 查询中时:

DELETE FROM courses AS C1
WHERE C1.`id` IN (
  SELECT
    ca.`id`
  FROM `course_attendees` AS ca
    LEFT JOIN `courses` c
      ON ca.`course_id` = c.`id`
  WHERE c.`id` IS NULL
);

我收到以下错误:

[2018-08-30 08:34:26] [42000][1064] 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 'AS C1

[2018-08-30 08:34:26] [42000][1064] 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 'AS C1
[2018-08-30 08:34:26] WHERE C1.`id` IN (
[2018-08-30 08:34:26] SELECT
[2018-08-30 08:34:26] ca.`id`
[2018-08-30 08:34:26] FROM `course_attendees` AS c' at line 1

由于 SELECT 查询有效,这里有什么问题,我该如何解决?

编辑

在 Tim 的回答之后,我陷入了这个错误:

[HY000][1093] You can't specify target table 'courses' for update in FROM clause

您的外部删除查询与子查询根本不相关,因此您在逻辑上不需要别名:

DELETE
FROM courses
WHERE id IN (
    SELECT id FROM
    (
        SELECT ca.id
        FROM course_attendees AS ca
        LEFT JOIN courses c
            ON ca.course_id = c.id
        WHERE c.id IS NULL
    ) t
);

我不确定在单个 table 上的删除语句中是否允许使用别名。允许他们进行删除连接,但您没有这样做。