从表中删除数据时收到错误#1242

时间:2013-03-18 10:45:49

标签: mysql sql database

我正在尝试从表中删除数据,我的查询如下:

delete from mybadge where uid=5 and badge_name=(select concat(tag_name,'-Beginner') from tag,post_tags where post_tags.tag_id=tag.tag_id);

我通过select语句得到5条记录,我想删除所有5条记录。

3 个答案:

答案 0 :(得分:2)

在关键字中使用 - '而不是'='符号

delete from mybadge where uid=5 and badge_name in (select concat(tag_name,'-Beginner') from tag,post_tags where post_tags.tag_id=tag.tag_id);

答案 1 :(得分:1)

如您所述,您的子查询返回5行。 但是,badge_name在同一行中不能有5个不同的值。这有点像说:

 delete from mybadge where uid=5 and badge_name=(5 different values)

delete from mybadge where uid=5 and badge_name in (select concat(tag_name,'-Beginner') from tag,post_tags where post_tags.tag_id=tag.tag_id);

答案 2 :(得分:0)

如果select语句对你有效,那么只需删除select:

返回的所有uid
DELETE FROM mybadge WHERE uid IN(
SELECT uid
FROM mybadge 
WHERE uid=5 
AND badge_name=(SELECT concat(tag_name,'-Beginner') 
                FROM tag,post_tags 
                WHERE post_tags.tag_id=tag.tag_id)
);