Skip to content
Advertisement

Select row by user_id and any row which share that entry’s event_id

I have a table which includes the following format:

 _______________________________
| event_id | user_id | username |
|-------------------------------|
|    30    |    1    |   user1  |
|    30    |    2    |   user2  |
|    30    |    3    |   user3  |
|    31    |    1    |   user1  |
|    31    |    4    |   user4  |
|    31    |    7    |   user5  |
|    32    |    3    |   user1  |
|    32    |    4    |   user4  |
|    32    |    5    |   user5  |
|_______________________________|

I considered storing users as JSON:

 _______________________________________________________
| event_id |                   users                    |
|-------------------------------------------------------|
|    30    | [{"user_id": 1, "username": "user1"}, ...] |
|    31    | [{"user_id": 1, "username": "user1"}, ...] |
|    32    | [{"user_id": 5, "username": "user5"}, ...] |
|_______________________________________________________|

But I imagine this would be awful on performance when trying to look up an event based on a JSON value.

I want to be able to look up any events where a username appears, and return all rows which have the same event_id. Is this possible to do in a single query (as I wish to minimize server strain), or should I just do a nested select?

Advertisement

Answer

Decided to just do a nested select that returns event_id; I initially did not do this as I thought the multiple statement queries option of node-mysql needed to be enabled for it, but it turns out that is not the case.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement