我有3个表格Tag,Software和Software_Tag,它们之间的关系是多对多。
我希望将子查询拆分为两部分(因为我无法在mysql中进行一次):
(SELECT softwares_id FROM Software_Tag GROUP BY softwares_id HAVING COUNT(*) >=2) as my
DELETE FROM Software_Tag WHERE tags_id=1 AND softwares_id IN (my)
在这里,我尝试使用名为“my”的虚拟表将其置于第二个查询中。 我怎么能这样做?
现在它像检查你的sql server 一样异常。
-
答案 0 :(得分:1)
要使用临时表,您需要这样做:
DROP TABLE IF EXISTS my_tmp;
CREATE TEMPORARY TABLE my_tmp SELECT softwares_id
FROM Software_Tag GROUP BY softwares_id
HAVING COUNT(*) >=2;
DELETE FROM Software_Tag
WHERE tags_id=1
AND softwares_id IN (SELECT softwares_id FROM my_tmp);
答案 1 :(得分:1)
你可以这样做:
DELETE FROM Software_Tag WHERE tags_id=1 AND softwares_id IN
(SELECT softwares_id FROM Software_Tag GROUP BY softwares_id HAVING COUNT(*) >=2)
这应该适用于MySQL。
编辑..试试这个
DELETE ST1
FROM Software_Tag ST1
JOIN (SELECT softwares_id FROM Software_Tag GROUP BY softwares_id HAVING COUNT(*) >=2) ST2 on ST1.softwares_id=ST2.softwares_id
where ST1.tags_id=1