我正在尝试从表中删除数据,我的查询如下:
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 条记录。
使用 - 'in'关键字而不是'='符号
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);
正如您所说,您的子查询返回 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);
如果 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)
);