Jump to content

Recommended Posts

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]";

Link to comment
https://forums.phpfreaks.com/topic/50198-avg-through-multiple-tables/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.