1

我有 3 张桌子。

myMembers
------------------------------------
id | username | privacy
------------------------------------
1  | userA    | 0
2  | userB    | 1
3  | userC    | 0
4  | userD    | 1

following
--------------------------------
id  | user_id  | follower_id
--------------------------------
1   | 2        | 1

posts
-------------------------------------
id  |  userID | username   | statusMsg
--------------------------------------
1   |  4      | userD    | Issac Newton is genius
2   |  2      | userB    | Newton Saw apple
3   |  3      | userC    | Newtonian Physics
4   |  1      | userA    | Calculus came from Sir Newton

有一个搜索字段。当登录用户在表“帖子”中搜索“关键字”时,我想省略那些将其隐私设置为“1”且搜索者未关注用户 B 的用户的结果。

查询在逻辑上应该这样做。

SELECT * from posts WHERE (match the keyword) 
            AND ( 
                if (poster's privacy (which is set in myMembers)==1){
                    if (seacher is following poster){
                        select this post
                    }
                }
                   else { select this post
                }
             )
                   LIMIT results to 5 rows

因此,对于关键字“Newton”,如果 userA 正在搜索,则应返回来自 'posts' 的第 2、3、4 行。如果 userD 正在搜索,则只应返回“posts”中的第 1、3 和 4 行,

基于隐私和以下编辑:标记以供将来搜索:mySql 中 WHERE 子句中的 IF 条件

4

3 回答 3

1

请尝试此查询(也在SQL Fiddle上):

SELECT p.id, p.user_id, m.username, m.privacy,
       searcher.username "Searcher", p.status_msg
  FROM posts p
  JOIN members m ON m.id = p.user_id
  LEFT JOIN following f ON p.user_id = f.user_id
  JOIN members searcher ON searcher.username = 'userA'
 WHERE (m.privacy = 0 OR (m.privacy = 1 AND f.follower_id = searcher.id)
                      OR m.id = searcher.id)
   AND p.status_msg LIKE '%New%'
 ORDER BY p.id
 LIMIT 5;

usernameposts表中删除了字段,因为它是多余的。此外,我对表和列的命名略有不同,因此查询可能需要对您的架构进行外观更改。

子句中的第一行WHERE是您要查找的行,它按以下顺序选择帖子:

  1. 来自没有隐私的成员的第一个帖子;
  2. 然后是当前成员的帖子searcher
  3. 最后是会员本人的帖子。

编辑:

此查询使用原始标识符:

SELECT p.id, p.`userID`, m.username, m.privacy,
       searcher.username "Searcher", p.`statusMsg`
  FROM posts p
  JOIN `myMembers` m ON m.id = p.`userID`
  LEFT JOIN following f ON p.`userID` = f.user_id
  JOIN `myMembers` searcher ON searcher.username = 'userD'
 WHERE (m.privacy = 0 OR f.follower_id = searcher.id OR m.id = searcher.id)
   AND p.`statusMsg` LIKE '%New%'
 ORDER BY p.id
 LIMIT 5;

编辑2:

为避免重复,以防posts表中的用户有多个关注者,连接和过滤条件应按以下方式更改(在SQL Fiddle上):

SELECT p.id, p.user_id, m.username, m.privacy,
       searcher.username "Searcher", p.status_msg
  FROM posts p
  JOIN members m ON m.id = p.user_id
  JOIN members searcher ON searcher.username = 'userC'
  LEFT JOIN following f ON p.user_id = f.user_id
   AND follower_id = searcher.id
 WHERE (m.privacy = 0 OR (m.privacy = 1 AND f.id IS NOT NULL)
                      OR m.id = searcher.id)  
 ORDER BY p.id
 LIMIT 5;
于 2012-08-13T11:14:23.710 回答
0

尝试这个:

SELECT a.*
FROM posts a
     LEFT JOIN (SELECT user_id
                FROM following a1
                     INNER JOIN myMembers b1
                       ON a1.follower_id = b1.id
                  WHERE a1.follower_id = 1 AND
                        b1.privacy = 1
                ) b
                ON a.userID = b.user_id AND
WHERE a.statusMsg LIKE '%search%' AND
      b.user_id IS NULL
LIMIT 5;

或没有子查询的更好方法:

SELECT a.*
FROM posts a
     LEFT JOIN myMembers b
         ON a.userID = b.id AND
            b.privacy = 1
     LEFT JOIN following c
         ON a.userID = c.user_id AND
            c.follower_id = 1
WHERE a.statusMsg LIKE '%search%' AND
      b.id IS NULL AND
      c.user_id IS NULL
LIMIT 5;

请参阅:SQL 连接的可视化解释

于 2012-08-13T04:23:02.953 回答
0

尝试以下操作:

SET @my_user_id= 1;

SELECT * FROM posts p
INNER JOIN myMembers m ON p.user_id= m.id  
WHERE statusMsg LIKE '%'
AND privacy=0
AND user_id IN (SELECT follower_id FROM following f WHERE f.user_id=@my_user_id)
LIMIT 5
于 2012-08-13T05:11:26.077 回答