I’m storing multiple numbers in a MySQL cell by using a delimiter (eg “1,5,10”) and the TEXT datatype.
How do I perform lookups like this?
SELECT * FROM MyTable WHERE MultiVals CONTAINS "5"
And lastly, is this the preferred approach to store such values? I’m using this like a linker table, to link certain rows to multiple other rows in another table (via IDs). Since I’m trying to minimize the filesize of the DB, I thought this would be a more compact approach to linking instead of using another table like this:
Person ID Product ID
----------- -----------
3 1
3 2
3 3
7 5
7 7
It’s advised to create a 3rd so called mapping table between the two linked table with two foreign key ID columns.
Using these, the two foreign keys acts as an index too. So it will speed up dramatically the lookup compared to a
FIND_IN_SETmethod.You are minimizing the file size, as the
TEXTdata type holds empty spaces for unused bits, while the numeric storage is the best, and uses minimal local storage.Anyway if you want to keep your original way you go this:
this will make sure the values are matched by using comma-s so a value of 5 will match just “5” and not “15”