所以我想要做的是,如果我的select中的结果与另一个post_id具有相同的post_data_id,则删除该结果但显示其中一个。我已经尝试了几天试图让这个工作,但失败了。请检查我的SQL Fiddle asweel作为我的意思的截图
由于
答案 0 :(得分:4)
可能有一种更简单的方法,但您可以使用:
SELECT p.*
FROM posts p
LEFT JOIN following f
ON f.user_id=1 AND p.post_user_id = f.follower_id
WHERE (post_user_id=1
OR f.follower_id IS NOT NULL)
AND (POST_DATA_ID NOT IN (SELECT POST_ID
FROM posts p
LEFT JOIN following f
ON f.user_id=1 AND p.post_user_id = f.follower_id
WHERE (post_user_id=1
OR f.follower_id IS NOT NULL))
OR POST_DATA_ID IS NULL)
ORDER BY `post_id` DESC;
答案 1 :(得分:1)
您无法在一个语句中真正删除和 SELECT。您将始终需要连续执行两个语句。
我不确定您要删除哪些行(重复的post_id或重复的post_data_id),但您需要以下四个语句中的两个:
SELECT * FROM posts WHERE post_data_id IN (
SELECT post_id FROM posts);
SELECT * FROM posts WHERE post_id IN (
SELECT post_data_id FROM posts);
DELETE FROM posts WHERE post_data_id IN (
SELECT post_id FROM posts);
DELETE FROM posts WHERE post_id IN (
SELECT post_data_id FROM posts);
答案 2 :(得分:0)
您可以使用NOT EXISTS并选择返回同一个表:
使用小提琴中的查询:
SELECT
p.*
FROM
posts p
LEFT JOIN following f
ON f.user_id=1 AND p.post_user_id = f.follower_id
WHERE
(post_user_id=1
OR f.follower_id IS NOT NULL)
and not exists (select 'x'
from posts posts_2
where p.post_data_id = posts_2.post_id)
ORDER BY `post_id` DESC
答案 3 :(得分:0)
试试这个。我为group by
添加了post_id
,因为您希望该列是唯一的。我假设如果post_type
相同,post_id
等列值保证相同,因此您可以使用某些函数安全地聚合它们,例如MAX
。
SELECT p.post_id, MAX(p.post_type), MAX(p.post_user_id), MAX(p.post_data_id)
FROM posts p
LEFT JOIN following f
ON f.user_id=1 AND p.post_user_id = f.follower_id
WHERE (post_user_id=1
OR f.follower_id IS NOT NULL)
GROUP BY p.post_id
ORDER BY `post_id` DESC;
答案 4 :(得分:0)
此查询存在相当多的问题,但我认为这个概念可能具有潜力:
SELECT -- p.*
GROUP_CONCAT(post_id) AS POST_IDs,
GROUP_CONCAT(post_type) AS POST_TYPEs,
GROUP_CONCAT(post_user_id) AS POST_USER_IDs,
GROUP_CONCAT(post_data_id) AS POST_DATA_IDs
FROM posts p
LEFT JOIN following f
ON p.post_user_id = f.follower_id
WHERE 1 IN (p.post_user_id, f.user_id)
GROUP BY LEAST(POST_ID,COALESCE(POST_DATA_ID, POST_ID))
ORDER BY post_id DESC;
主要问题是知道在给定行中使用哪个POST_ID或POST_TYPE或POST_USER_ID。
答案 5 :(得分:0)
这是获取指定结果的一种方法,使用原始查询作为内联视图。 (实际上,查询引用了原始查询两次。我从原始查询中删除了ORDER BY。不需要删除它,但它也没有必要在那里。
SELECT a.*
FROM (
SELECT p.*
FROM posts p
LEFT JOIN following f
ON f.user_id=1 AND p.post_user_id = f.follower_id
WHERE (post_user_id=1
OR f.follower_id IS NOT NULL)
) a
LEFT
JOIN (
SELECT p.*
FROM posts p
LEFT JOIN following f
ON f.user_id=1 AND p.post_user_id = f.follower_id
WHERE (post_user_id=1
OR f.follower_id IS NOT NULL)
) b
ON b.post_id = a.post_data_id
WHERE b.post_id IS NULL
ORDER BY a.post_id DESC;
可能有更有效的方法来获得相同的结果,但此查询满足要求。它使用经典的angi-join模式:LEFT [OUTER] JOIN
来查找匹配的行,并使用WHERE
子句中的谓词来消除匹配的行。
e.g。从p获取行,其中q
中没有匹配的行SELECT p.*
FROM p
LEFT
JOIN q
ON q.id = p.q_id
WHERE q.id IS NULL
答案 6 :(得分:-1)
SELECT p.*
FROM posts p
LEFT JOIN following f
ON f.user_id=1 AND p.post_user_id = f.follower_id
LEFT JOIN post x
ON p.post_data_id=x.post_id
WHERE (p.post_user_id=1
OR f.follower_id IS NOT NULL)
AND x.post_id is null
ORDER BY p.post_id DESC;