Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lara28580's avatar

Group by or seperate table?

I have a question about designing my db and when it could be useful to create a new table. Let's say I have a hasMany relation and I want to get the rows of a specific column key with groupBy. Till now not a problem, but what is if I want to display the groupBy as 1 result in my view. To be more clear 3 rows with different paths to images and I want to display those 3 objects as one result in my view. Or is it better to create a new table with those paths and retrieve them if needed? Because its unhandy to group the result every time I need them in view?

Table with paths (option 1)

| id  | path | column       |
| ----| --------|-----------------|
| 1   | same| something |
| 2   | same| something |

Tables in relation (option 2)

| id  | path | column       |
| ----| --------|----------------|
| 1   | path | something |
| id  | foreign_key |        path    | column       |
| ----| ----------------- |----------------|-----------------|
| 1   |     1                |        path   | something  |
| 2   |     1                |        path   | something  |
0 likes
1 reply
bobbybouwmann's avatar
Level 88

The grouping only makes sense most of the time to that by a certain state, number, or date. In your case, it sounds that your object can have one or many paths. So it would make sense to make this a separate table that stores the paths for that specific object.

Another solution might be using a JSON column with multiple paths in there, but that depends on what you're doing with them.

1 like

Please or to participate in this conversation.