donglian4464 2013-11-13 10:58
浏览 34
已采纳

混淆Avg()和加入2个表

I've got myself into a bit of a tiss over averaging and joining tables.

Essentially I want to display the average heights of different plant species using Highcharts, pulling the data from a MySQL database. Unfortunately the height data and the species names were setup to be added in different tables.

I've got it working, however when I download the data and find the averages in Excel the figures are different to those being displayed - so I'm obviously not doing it right. I've double checked I'm doing it right in Excel so almost certain it's my MySQL query that's stuffing up.

There's loads of entries in the actual tables, so I've just put an example below.

The query I have at the moment is:

<?php
$result = mysql_query("
SELECT DISTINCT(plant_records.plant_id), ROUND(AVG(plant_records.height),2) as plant_average, plant_list.id, plant_list.plant_species
FROM plant_records
INNER JOIN plant_list
ON plant_records.plant_id=plant_list.id
GROUP BY plant_list.plant_species
")  or die(mysql_error()); 

while ($row = mysql_fetch_array($result)) {
$xAxisValues[] = "'" . $row['plant_species'] . "'";
$AseriesValues[] = $row['plant_average'];
}
?>

Am I doing it right? I found some nice tutorials explaining joins, like this one, but I'm still confused. I'm wondering if I'm averaging before I've joined them, or something??

"plant_id" in the Records table corresponds with "id" in the List table

plant_records:

id  plant_id    date_recorded   height
1   3           01/01/2013      0.2523123
2   1           02/01/2013      0.123
3   3           03/02/2013      0.446
4   3           04/03/2013      0.52
5   1           05/03/2013      0.3
6   2           06/03/2013      0.111
7   2           07/05/2013      0.30
8   4           08/05/2013      0.22564
9   1           09/05/2013      1.27
10  3           10/05/2013      1.8

plant_list:

id  registration_date   contact_name    plant_species   plant_parent
1   01/01/2013          Dave            ilex_prinos     London_Holly
2   02/01/2013          Bill            acer_saccharum  Brighton_Maple
3   01/01/2013          Bob             ilex_prinos     London_Holly
4   04/01/2013          Bruno           junip_communis  Park_Juniper

EDIT: I've tried every possible way of finding the data using Excel (e.g. deliberately not filtering unique IDs, different average types, selecting multiple species, etc) to find the calculation my query is using, but I can't get the same results.

  • 写回答

2条回答 默认 最新

  • dongni8969 2013-11-13 13:14
    关注

    I notice two issues with your query at the moment.

    1. Selecting plant_list.id while having a GROUP BY plant_list.plant_species will not yield anything of interest, due to the fact that MySQL will return an arbitrary id from any of the plants that match each species.

    2. You state that you are only interested in the most recent recording, but nothing in your query reflects that fact.

    Given that information, try this query:

    SELECT ROUND(AVG(pr.height),2) as plant_average, plant_list.plant_species
    FROM plant_records pr
    INNER JOIN plant_list
    ON pr.plant_id=plant_list.id
    WHERE pr.date_recorded = (
        SELECT MAX(pri.date_recorded) FROM plant_records pri
        WHERE pri.plant_id = pr.plant_id
    )
    GROUP BY plant_list.plant_species
    

    Alternately, if you want just the average heights for a specific date, simply pass that directly into the query, instead of using the subquery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。