Skip to content

Instantly share code, notes, and snippets.

@jameslittle230
Last active April 20, 2018 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameslittle230/4bf12d3190a75c334bac673fe48abc1f to your computer and use it in GitHub Desktop.
Save jameslittle230/4bf12d3190a75c334bac673fe48abc1f to your computer and use it in GitHub Desktop.

I have three tables that store data like this:

chats

chat_id sqn_num name
1 5 My Cool Chat
2 3 Another Awesome Chat
3 9 A Third Rockin' Chat

user_chat

chat_id user_id chat_id
1 1 1
2 1 2
3 2 2
4 2 3
5 3 3
6 3 1

users

user_id name
1 James Little
2 Maddie Tucker
3 Danny Little

And I want a function where I can input user_id = 1 so it outputs data like this:

[
  {
    "chat_id": 1,
    "sqn_num": 5,
    "chat_name": "My Cool Chat",
    "members": [
      {
        "user_id": 1,
        "name": "James Little"
      }
      {
        "user_id": 2,
        "name": "Maddie Tucker"
      },
    ]
  },
  {
    "chat_id": 3,
    "sqn_num": 9,
    "chat_name": "A Third Rockin' Chat",
    "members": [
      {
        "user_id": 1,
        "name": "James Little"
      },
      {
        "user_id": 3,
        "name": "Danny Little"
      }
    ]
  }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment