-1

我有 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);

请问有什么想法吗?

4

1 回答 1

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;
于 2013-08-09T18:51:51.603 回答