I have a table in MySQL called bundles
, this table has a column called attractions_array
which is a comma separated list of "attraction ids" e.g 41,13,60
attractions
is another table in the database that has all the info about each of these attractions.
I have figured out how to get the first id from the array using: substring_index(attractions_array,',',1)
but this isn't too helpful.
I somehow need to check through each of these ids from their own attractions
table to do things like:
- Average & total price for all of the attractions
- Make sure the attraction is still available
- etc...
How would this be possible in MySQL? I don't even know where to start looking.
If MySQL was PHP I would probably write something like this to get the job done:
foreach ($bundles as $bundle) {
// init pricing
$bundle->price_count = 0;
$bundle->price_total = 0;
// each of the attractions
$attractions = explode(",", $bundle->attractions_array);
foreach ($attractions as $attraction) {
// no longer available
if (!$attraction->available) {
continue 2;
}
// count pricing
$bundle->price_count++;
$bundle->price_total += $attraction->price;
}
// average pricing
$bundle->price_average = $bundle->price_total / $bundle->price_count;
}
My current statement
SELECT * FROM `bundles` LIMIT 0,10
Is something like this even possible in MySQL?