So I am creating sections for a store. The store can have multiple scopes, if there isn't a section_identifier
set for a given store_id
it should fallback to the global store which is 0
.
The SQL command that I want should return a list of section_options
for any related given store.
Example of my table:
SELECT * FROM my_table:
+----+--------------------+----------------------+----------+
| id | section_identifier | option_identifier | store_id |
+----+--------------------+----------------------+----------+
| 17 | header | header_option_one | 1 |
| 18 | footer | footer_option_one | 0 |
| 19 | homepage_feature | homepage_feature_one | 0 |
| 23 | header | header_option_three | 0 |
| 25 | homepage_feature | homepage_feature_one | 1 |
+----+--------------------+----------------------+----------+
So section_identifier
is unique, the IDs I need back for store 1 would be 17, 18 and 25.
When I run:
SELECT * FROM my_table GROUP BY section_identifier
it returns:
+----+--------------------+----------------------+----------+
| id | section_identifier | option_identifier | store_id |
+----+--------------------+----------------------+----------+
| 18 | footer | footer_option_one | 0 |
| 23 | header | header_option_three | 0 |
| 19 | homepage_feature | homepage_feature_one | 0 |
+----+--------------------+----------------------+----------+
This means if I run SELECT * FROM my_table GROUP BY section_identifier DESC
:
I get the response (this is my desired output):
+----+--------------------+----------------------+----------+
| id | section_identifier | option_identifier | store_id |
+----+--------------------+----------------------+----------+
| 25 | homepage_feature | homepage_feature_one | 1 |
| 17 | header | header_option_one | 1 |
| 18 | footer | footer_option_one | 0 |
+----+--------------------+----------------------+----------+
Although this works, I have no understanding of as to why.
Its my understanding the initial GROUP BY
should get the first instance in the database, IE the response I expect should be:
+----+--------------------+----------------------+----------+
| id | section_identifier | option_identifier | store_id |
+----+--------------------+----------------------+----------+
| 18 | footer | footer_option_one | 0 |
| 17 | header | header_option_three | 1 |
| 19 | homepage_feature | homepage_feature_one | 0 |
+----+--------------------+----------------------+----------+
However, it seems to be referencing my store_id
somehow? I have tried a few different combinations and Im weirdly getting my expected result each time but I have no understanding as to why.
Can anybody explain this to me please?
PS
I have tried updating the option_identifier
of id = 7
to see if MySql references the latest saved on disk and it didn't change the result.
Also: I'm not planning on using this feature or asking for an alternative, I'm asking what's going on with it?