如何修改此工作 SQL SELECT/JOIN 查询以删除重复项?
Posted
技术标签:
【中文标题】如何修改此工作 SQL SELECT/JOIN 查询以删除重复项?【英文标题】:How to modify this working SQL SELECT/JOIN query to DELETE the duplicates? 【发布时间】:2015-10-04 20:48:56 【问题描述】:我无法修改它以删除重复记录。在这种情况下,对于给定的 meta_key,我有三个具有相同 meta_value 的帖子,它返回了两个结果(我可能对 GROUP BY 有问题——仍在解决它)。但是,将第一个 SELECT 更改为 DELETE 不起作用。这不是我要更改的部分,以便从两个表中删除数据吗?
$sql1 = "SELECT *
FROM (
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_type, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta
WHERE wp_posts.ID=wp_postmeta.post_ID
AND post_type='banners'
AND post_status='publish'
AND meta_key='uselink'
AND meta_value='http://www.mylink.com/subpage/')
AS a
INNER JOIN (
SELECT meta_value, MIN( post_id )
AS min_id
FROM wp_postmeta
GROUP BY meta_value
HAVING COUNT( * ) > 1 )
AS b
ON b.meta_value = a.meta_value
AND b.min_id <> a.id";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0)
while($row = $result1->fetch_assoc())
echo $row["meta_value"] . ", ";
else
echo "0 results";
这是我尝试过的(不工作):
$sql1 = "DELETE *
FROM (
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_type, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta
WHERE wp_posts.ID=wp_postmeta.post_ID
AND post_type='banners'
AND post_status='publish'
AND meta_key='uselink'
AND meta_value='http://www.mylink.com/subpage/')
AS a
INNER JOIN (
SELECT meta_value, MIN( post_id )
AS min_id FROM wp_postmeta
GROUP BY meta_value HAVING COUNT( * ) > 1 )
AS b
ON b.meta_value = a.meta_value
AND b.min_id <> a.id";
在没有第二个可用的连接表的情况下,我将关闭这个:
DELETE a.*
FROM wp_posts
AS a
INNER JOIN (
SELECT post_title, MIN( id )
AS min_id
FROM wp_posts
WHERE post_type = 'banners'
AND post_status = 'publish'
GROUP BY post_title HAVING COUNT( * ) > 1 )
AS b
ON b.post_title = a.post_title AND b.min_id <> a.id
AND a.post_type = 'banners'
AND a.post_status = 'publish'
【问题讨论】:
select 和 delete 查询的区别是SELECT * FROM table WHERE this=that
和 Delete FROM table WHERE this=that
,所以你看到用 select 替换 just delete 是行不通的
我添加了这个问题只是为了帮助我把它放在我的脑海里。我只是不清楚如何处理,因为只有一个表只是将第一个 SELECT 更改为 DELETE 确实有效;不知道是不是跟有两张表有关?
我加入表格后是否需要删除?是这个问题吗?
首先你必须从 DELETE QUERY 中删除 *
,它应该看起来像 DELETE FROM
而不是 DELETE * FROM
第二你不能在 InnerJoin 中选择,只需使用 WHERE 子句加入表格即可去
SQL DELETE with INNER JOIN 的可能重复项
【参考方案1】:
试试这个,(但我不确定)
$sql1 = "DELETE p, pm
FROM wp_posts p
INNER JOIN wp_postmeta pm ON p.ID=pm.post_ID
WHERE p.post_type='banners'
AND p.post_status='publish'
AND pm.meta_key='uselink'
AND pm.meta_value='link'
AND (SELECT count(pm.meta_value) FROM
wp_postmeta pm
GROUP BY pm.meta_value
HAVING COUNT(*) > 1
WHERE pm.meta_value=p.meta_value)";
【讨论】:
感谢您的跟进;我可以遵循我认为的那种逻辑。我一定是误会了重复计数检查。我想查找并删除那些重复项。选择返回了它们,但我就是不知道如何整合 INNER JOIN/HAVING COUNT 部分 例如,对于我一直在尝试的所有不同迭代,这确实会根据 WHERE 条件删除帖子,但现在我只需要删除重复项:$sql1 = "DELETE p, pm FROM wp_posts p INNER JOIN wp_postmeta pm ON p.ID=pm.post_ID WHERE p.post_type='banners' AND p.post_status='publish' AND pm.meta_key='uselink' AND pm.meta_value='mylink.com/subpage'"; 很抱歉没有回来查看;我仍在努力... 5 天,数...谢谢您的指导 - 必须接近以上是关于如何修改此工作 SQL SELECT/JOIN 查询以删除重复项?的主要内容,如果未能解决你的问题,请参考以下文章