使用mysql查询重复结果

时间:2013-08-09 18:21:06

标签: mysql duplicates

我要查询3个表。我根据另外两个来选择第一个。我必须只有第一个表中的不同ID,但我的查询返回一些重复... http://sqlfiddle.com/#!2/3e3d6/1

我的查询:

SELECT p.* 
    FROM posts p, blogs_subscribed s
    WHERE (p.user_id = s.user_id OR p.user_id = 1)
      AND p.id NOT IN (
      SELECT post_id 
      FROM posts_unsubscribed u
      WHERE u.post_id = p.id
         AND u.user_id = p.user_id);

SELECT p.* 
    FROM posts p, blogs_subscribed s
    WHERE (p.user_id = s.user_id OR p.user_id = 1)
      AND NOT EXISTS(
      SELECT null 
      FROM posts_unsubscribed u
      WHERE u.post_id = p.id 
        AND u.user_id = p.user_id);

请问好吗?

1 个答案:

答案 0 :(得分:0)

不完全确定我理解你在寻找什么,但我认为这就是你想要的......

SELECT p.* 
    FROM posts p, blogs_subscribed s
    WHERE (p.user_id = s.user_id OR p.user_id = 1)
      AND p.id NOT IN (
      SELECT post_id 
      FROM posts_unsubscribed u
      WHERE u.post_id = p.id
         AND u.user_id = p.user_id)
      GROUP BY p.id;

SELECT p.* 
    FROM posts p, blogs_subscribed s
    WHERE (p.user_id = s.user_id OR p.user_id = 1)
      AND NOT EXISTS(
      SELECT null 
      FROM posts_unsubscribed u
      WHERE u.post_id = p.id 
        AND u.user_id = p.user_id)
      GROUP BY p.id;