kernelgpf Posted May 6, 2007 Share Posted May 6, 2007 I'm trying to get the average of several columns [all with the name of SIR] in several different tables, and display the result. My code is giving me a "please check the manual depending on your MySQL version.." error. Here's my code- any help would be appreciated. $query=mysql_query("select AVG(barenas.SIR,caves.SIR,tanks.SIR,training_areas.SIR,vet_clinics.SIR,grooming_centers.SIR,shops.SIR) from barenas,caves,tanks,training_areas,vet_clinics,grooming_centers,shops"); $row=mysql_fetch_array($query); echo "<li>your district's average safety inspection rating is <b>".$row['AVG(SIR)']."</b> [this is based upon individual building SIRs in your district]"; Quote Link to comment https://forums.phpfreaks.com/topic/50198-avg-through-multiple-tables/ Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 Whats the Error message its showing. Quote Link to comment https://forums.phpfreaks.com/topic/50198-avg-through-multiple-tables/#findComment-246524 Share on other sites More sharing options...
Barand Posted May 6, 2007 Share Posted May 6, 2007 A couple of things 1 ) you can't specify multiple cols in and AVG(). Need to be AVG(barenas.SIR) , AVG(caves.SIR) ,... etc. 2 ) if you specify a join like this from barenas,caves,tanks,training_areas,vet_clinics,grooming_centers,shops you join every record in each table with every record in each each other table so if each of the seven tables has 10 records, you query 10,000,000 rows! You could try SELECT 'grooming_centers' as name, AVG(SIR) as sir FROM grooming_centers UNION SELECT 'barenas' as name, AVG(SIR) as sir FROM barenas UNION SELECT 'caves' as name, AVG(SIR) as sir FROM caves UNION SELECT 'tanks' as name, AVG(SIR) as sir FROM tanks UNION SELECT 'training_areas' as name, AVG(SIR) as sir FROM training_areas UNION SELECT 'vet_clinics' as name, AVG(SIR) as sir FROM vet_clinics UNION SELECT 'shops' as name, AVG(SIR) as sir FROM shops Quote Link to comment https://forums.phpfreaks.com/topic/50198-avg-through-multiple-tables/#findComment-246572 Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 THanks I didn't know the AVG() function. Quote Link to comment https://forums.phpfreaks.com/topic/50198-avg-through-multiple-tables/#findComment-246588 Share on other sites More sharing options...
kernelgpf Posted May 7, 2007 Author Share Posted May 7, 2007 That worked wonders, thanks Barand! =] Quote Link to comment https://forums.phpfreaks.com/topic/50198-avg-through-multiple-tables/#findComment-247624 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.