我需要构建一个MySQL查询,以便在将评论添加到post时通知用户软件包。我有几张桌子
Posts:pid,uid 注释:cid,uid,pid 用户:uid 与相关的:rid,uid1,uid2 设备:did,uid
一个帖子可以有很多评论,很多评论可以发布给每个用户,一个用户可能有很多设备。
当一个评论被插入,我需要得到一个独特的设备的列表,每个人谁已经作出了目前的评论,包括作者,但只有当他们是与用户的评论。
这个
redlated.uid1
保存用户and和
related.uid2
保存它们的相关用户标识。
-----------------------------------
| rid | uid1 | uid2 |
-----------------------------------
| 1 | 34 | 43 |
-----------------------------------
| 2 | 43 | 34 |
-----------------------------------我有以下数据
PostAuthorId、、PostID、、UserID
有人能帮我找到正确的数据吗?到目前为止,所有的查询都是错误的,我没有发布一个,因为我认为这可能会进一步混淆。
就像我说的,我只需要返回唯一的设备,任何帮助都是很棒的,谢谢。
** 编辑SQLFIDDLE
发布于 2012-08-09 16:07:10
您所需要的只是一个正确的(多)连接。
SELECT ...
FROM Posts P
INNER JOIN Users O ON P.uid = O.uid -- Post owner
INNER JOIN Devices OD ON O.uid = OD.uid -- Devices of post owner
INNER JOIN Related R ON O.uid = R.uid1 -- Post owner to other user relationship
INNER JOIN Comments C ON R.uid2 = C.uid AND P.pid = C.pid -- Comments by related user
INNER JOIN Devices CD ON C.uid = CD.uid -- Devices of users posting comments
WHERE pid = yourPostID这样就行了。
不要忘记您有两次设备表,所以您必须通过OD (所有者)和CD (评论海报)设备来引用它们
发布于 2012-08-09 16:11:28
select distinct d.did from devices d where d.uid in
(select c.uid from comments c where
c.pid = (select c.pid from comments c where c.cid=@recently_inserted_cid)
union
(select p.uid from posts p where
p.pid = (select c.pid from comments c where c.cid=@recently_inserted_cid))
)
and d.uid in
(select r.uid2 from related r where r.uid1 =
(select c.uid from comments c where c.cid=@recently_inserted_cid))https://stackoverflow.com/questions/11886985
复制相似问题