MySQL - 从两个表中删除项目

时间:2015-07-09 12:55:55

标签: mysql sql

我试图删除项items表。如果它存在,也来自saves表。

这是错误:

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 'i LEFT JOIN saves s ON i.id = s.item_id

DELETE FROM items i
LEFT JOIN saves s
ON i.id = s.item_id
WHERE i.id = ? AND s.item_id = ?
AND
NOT EXISTS (SELECT id FROM pending_wins WHERE item_id = ?)
AND
NOT EXISTS (SELECT id FROM bids WHERE item_id = ?)

4 个答案:

答案 0 :(得分:0)

如果您想从单个表中删除,请说items试试这个::

DELETE i FROM items i
LEFT JOIN saves s
ON i.id = s.item_id
WHERE i.id = ? AND s.item_id = ?
AND
NOT EXISTS (SELECT id FROM pending_wins WHERE item_id = ?)
AND
NOT EXISTS (SELECT id FROM bids WHERE item_id = ?)

如果要从两个表中删除记录,请尝试以下::

DELETE i, s FROM items i
LEFT JOIN saves s
ON i.id = s.item_id
WHERE i.id = ? AND s.item_id = ?
AND
NOT EXISTS (SELECT id FROM pending_wins WHERE item_id = ?)
AND
NOT EXISTS (SELECT id FROM bids WHERE item_id = ?)

答案 1 :(得分:0)

DELETE i, s 
FROM items i
LEFT JOIN saves s
ON i.id = s.item_id
WHERE s.item_id = ?
AND
 NOT EXISTS (SELECT id FROM pending_wins WHERE item_id = ?)
AND
 NOT EXISTS (SELECT id FROM bids WHERE item_id = ?)

答案 2 :(得分:0)

使用以下查询删除表i中的数据:

DELETE i FROM items i
LEFT JOIN saves s
ON i.id = s.item_id
WHERE i.id = ? AND s.item_id = ?
AND
NOT EXISTS (SELECT id FROM pending_wins WHERE item_id = ?)
AND
NOT EXISTS (SELECT id FROM bids WHERE item_id = ?)

答案 3 :(得分:0)

您只需在查询中i之前添加FROM

DELETE i FROM items i
LEFT JOIN saves s
ON i.id = s.item_id
WHERE i.id = ? AND s.item_id = ?
AND
NOT EXISTS (SELECT id FROM pending_wins WHERE item_id = ?)
AND
NOT EXISTS (SELECT id FROM bids WHERE item_id = ?)