For an app like iOS Messages:
How should I design the server database queries & schema?
Queries
- Create a user.
- Get a user's chats.
- Get the messages between two users.
- Add a message to a chat (creating the chat if it doesn't yet exist).
Note:
- Every chat only has two users (no group chats).
- Every chat's
id
equals this unique unordered pairing function applied touser_id
&with_id
.
How should I query a user's chats, each with its latest message?
Example User Interface (UI)
See my design & query on SQLFiddle. It works, but how can I make it better?