I am storing relational items in fields as comma delimited IDs like so:
,4,12,8,16,198,
The reason for the leading and trailing commas are for searching with LIKE '%,id,%'
I am trying to write a bootstrap function to retrieve all these items based on the ID in the order of the IDs. My question is what is the most efficient way of retrieving these rows? The options I have thought of are:
- Explode into array, loop over array and do individual select queries (Too many queries?)
- One select using IN() (I don’t think this will maintain the order?)
- Select all items within the related table and then choose from that array based on IDs (benefit might be having only one query?)
Perhaps it should be mentioned that I am using PDO, so I can prepare statements once and then execute them several times for different IDs… although I am unaware how much that affects performance.
Any help would be greatly appreciated!
Thank you.
I’d go for
in combination with the
IN