我有以下MySQL查询,我正在使用PHP(5.2).
DELETE t1.*, t3.* FROM
forum_posts AS t1,
forum_topics AS t2,
user_points AS t3
WHERE t1.topic_id = t2.topic_id
AND t2.deleted = 1
AND t1.post_id = t3.id
AND t3.type = 'post'
AND t1.post_author = t3.profile_author
Run Code Online (Sandbox Code Playgroud)
然而,它也没有发挥作用,我也打算这样做(没有任何反应!),让我解释一下:
我想要查询的是删除forum_posts表中的所有行和/或(我说"和/或"因为它将依赖于行甚至存在)删除user_points表中的所有行=> [如果创建帖子的主题已被删除(为了避免混淆,您的信息实际上只是"隐藏",我们通过检查它是否等于确定1)].
我希望查询结构自我解释,topic_id主要用于JOIN表格.
查询运行正常(没有给出MySQL或PHP错误,所以我假设语法没问题?),我已经检查了DB和theres主题,这些主题被删除(deleted列被设置为1)以及它们的帖子是否存在以及对于那些主题(所以不存在没有数据的情况).
感谢所有回复.
我个人喜欢总是明确指定连接,而不是将所有内容都放在where子句中.这也有助于可视化您可能需要LEFT联接的位置.
DELETE forum_posts t1, user_points t3
FROM forum_posts AS t1
INNER JOIN forum_topics AS t2 ON t2.topic_id = t1.topic_id
INNER JOIN user_points AS t3 ON t3.id = t1.post_id
AND t3.profile_author = t1.post_author
AND t3.type = 'post'
WHERE t2.deleted = 1
Run Code Online (Sandbox Code Playgroud)
现在,基于您的陈述,我建议将第二个INNER JOIN更改为LEFT JOIN:
DELETE forum_posts t1, user_points t3
FROM forum_posts AS t1
INNER JOIN forum_topics AS t2 ON t2.topic_id = t1.topic_id
LEFT JOIN user_points AS t3 ON t3.id = t1.post_id
AND t3.profile_author = t1.post_author
AND t3.type = 'post'
WHERE t2.deleted = 1
Run Code Online (Sandbox Code Playgroud)
我希望有所帮助!