I have a DB with several tables that contain basic, static ID-to-name data. 2 Columns only in each of these reference tables.
I then have another table that will be receiving data input by users. Each instance of user input will have it’s own row with a timestamp, but the important columns here will contain either one, or several of the ID’s related to names in one of the other tables. For the ease of submitting and retrieving this information I opted to input it as text, in json format.
Everything was going great until I realized I’m going to need to Join the big table with the little tables to reference the names to the ID’s. I need to return the IDs in the results as well.
An example of what a few rows in this table might look like:
Column 1 | Column 2 | Timestamp
["715835199","91158582","90516801"] | ["11987","11987","22474"] | 2012-08-28 21:18:48
["715835199"] | ["0"] | 2012-08-28 21:22:48
["91158582","90516801"] | ["11987"] | 2012-08-28 21:25:48
There WILL be repeats of the ID#’s input in this table, but not necessarily in the same groupings, hence why I put the ID to name pairings in a separate table.
Is it possible to do a WHERE name='any-of-these-json-values'? Am I best off doing a ghetto join in php after I query the main table to pull the IDs for the names I need to include? Or do I just need to redo the design of the data input table entirely?
First of all:
Never, ever put more than one information into one field, if you want to access them seperately. Never.
That said, I think you will need to create a full
N:Mrelation, which includes a join table: One row in your example table will need to be replaced by 1-N rows in the join table.A tricky join with string matching will perform acceptably only for a very small number of rows, and the
WHERE name='any-of-these-json-values'is impossible in your construct: MySQL doesn’t “understand”, that this is a JSON array – it sees it as unstructured text. On a join table, this clause comes quite naturally asWHERE somecolumn IN (1234,5678,8012)Edit
Assuming your
Column 1contains arrays of IDs intable1andColumn 2carries arrays of IDs intable2you would have to do something like(you might want to sanity-check the keys)
And on an insert do the following (in pseudo-code)
So for your third example row, the SQL would be: