MySQL并使用INNER JOIN删除多行

时间:2017-07-31 10:34:12

标签: mysql sql database

我正在使用MySQL。我想删除chart_id = 12places.match_no > 104

的所有匹配项

无论如何,我的查询无效。我看不出是什么原因。

DELETE FROM matches
  INNER JOIN places ON places.id = matches.place_id
    AND places.match_no > 104
WHERE matches.chart_id = 12

这会导致错误:

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第2行的“INNER JOIN place ON places”附近使用正确的语法

3 个答案:

答案 0 :(得分:2)

您需要指定要删除的表格:

DELETE m
    FROM matches m INNER JOIN
         places p
         ON p.id = m.place_id AND p.match_no > 104
    WHERE m.chart_id = 12;

表名/别名介于DELETEFROM

之间

documentation

清楚地解释了这一点
  

多表语法

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    tbl_name[.*] [, tbl_name[.*]] ...
    FROM table_references
    [WHERE where_condition]

答案 1 :(得分:1)

另一种选择是:

DELETE FROM matches
WHERE matches.chart_id = 12 and matches.place_id in 
(select place_id 
 from places 
 where match_no > 104
)

答案 2 :(得分:1)

试试这个:

    DELETE m
    FROM matches m INNER JOIN
    places p
    ON p.id = m.place_id
    WHERE m.chart_id = 12 AND p.match_no > 104