Essentially I have some user generated meta data for individual items. Meta data consists of a title and a corresponding value. One item may have one particular metadata title while another may not. So, creating a new collumn in my main table seems like the wrong approach, since I don’t want every item to have the same meta data titles.
The problem is when I want to sort by that meta data. I can easily generate a table that makes the metadata look like just another collumn, but I want to sort by that to. So, to illustrate what I have:
Say I have the following tables:
Items Table:
id | color
----------
0 | red
1 | pink
2 | orange
MetaData Table
itemId | title | data
-----------------------------------------
0 shape round
1 | shape | square
0 | sound | LOUD!
I can easily write the SQL and code to generate something like the following from those tables:
id | color | shape | sound
--------------------------------------------
0 | red | round | LOUD!
1 | pink | square |
2 | orange | |
So, when generating that, if a item doesn’t have a metadata title I’m displaying, then it’s just left blank. I would like to allow the user to sort by shape or sound and just have the ones that are blank show up at the bottom.
I could bring in all the data, generate the tables, then sort it in code..but…that wouldn’t be practical at all for hundreds of entries that I may need to work with.
Is this possible to do in SQL (I’m using mysql, but something more generic would be better in case I change that)? Is my table layout just impractical for this/should I do it a different way (I’d prefer not to change my table layout).
Thanks!
You can put the blank values at the bottom with something like this: