I want to store multiple id(int) in one row, I only see Varchar for this, and I use commas.
The reason is to improve efficiency:
While inserting/updating, this id's are changed periodically, so if I store 40 id's, instead of doing 40 deletes and 40 inserts, I just do 1 Update.
Then I use this data to make a select query with an IN(id,id2,id3) if I select 40 rows (in a 40x bigger index or table column) with the inefficient method it works well, but I prefer to do the 1 row method, the problem is that it is an string, 'id,id2,id3' and it does not work, it only converts to int the first number, the same that CAST or CONVERT to int does.
I found a method that uses a second table but you have to store all the id’s, that’s not possible there are too many and they are created every second, that would be inefficient.
I think there should be something, it's so simple and so useful, but I'm not finding any efficient and simple way to convert the string to a comma separated integer list.
In summary: I want to do this, being varch_ids a varchar '1,2,3,4' in 1 row
SELECT * FROM main_table
WHERE main_id IN (SELECT varch_ids FROM table2 WHERE data_id=$x)
Yes I can do two separated queries and process the string, but the most important and used query is the SELECT, and two queries would probably be slower than selecting 40 rows in 1 query.