I have a need to build a MySQL query to notify users of a software package when a comment is added to a post. The tables I have for this are
Posts: pid, uid
Comments: cid, uid, pid
Users: uid
Related: rid, uid1, uid2
Devices: did, uid
A post can have many comments, many comments can be posted per user, and a user may have many devices.
When a comment is inserted I need to get a list of unique devices for everyone who has contributed to the comments for the moment including the author but only where they are related to the user who has commented.
the
redlated.uid1
holds the userid and
related.uid2
holds their related userid e.g.
-----------------------------------
| rid | uid1 | uid2 |
-----------------------------------
| 1 | 34 | 43 |
-----------------------------------
| 2 | 43 | 34 |
-----------------------------------
I have available the following data
PostAuthorId | PostID | UserID
Can anyone help I am struggling to get the right data out all queries thus far have been wrong, i have not posted one as I think it could confuse even further.
As i said I need to return only unique devices, any help would be awesome thanks
EDIT SQLFIDDLE **
http://sqlfiddle.com/#!2/4217f
All you need is one correct (multi) JOIN
this should do it.
Don’t forget that you have Devices table twice so you have to refer to them via OD (owner’s) and CD (comment poster’s) devices