Php 从一个表中为另一个表中的特定行获取的平均值

Php 从一个表中为另一个表中的特定行获取的平均值,php,mysql,Php,Mysql,我正在努力做到以下几点 从名为Hospital的表中选择id=hid的医院。 向其添加一个值,并从另一个名为hrating的表中获取所有评级并对其进行平均 这是我的问题 $statement = $conn->prepare('SELECT hospital.*,(SELECT AVG(hrating.rating_h) FROM hrating WHERE hid = hospital.hid) as overall_rating WHERE hid=:hid LIMIT 1');

我正在努力做到以下几点

从名为Hospital的表中选择id=hid的医院。 向其添加一个值,并从另一个名为hrating的表中获取所有评级并对其进行平均

这是我的问题

 $statement = $conn->prepare('SELECT hospital.*,(SELECT AVG(hrating.rating_h) FROM hrating WHERE hid = hospital.hid) as overall_rating WHERE hid=:hid LIMIT 1');
获取此错误

{"error":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE hid='44' LIMIT 1' at line 1"}

我哪里错了。

您的查询中似乎没有来自医院的信息

SELECT hospital.*,(SELECT AVG(hrating.rating_h) 
                   FROM hrating 
                   WHERE hid = hospital.hid) as overall_rating 
FROM hospital -- this line seems to be missing ??
WHERE hid=:hid LIMIT 1
试试这个:

选择医院。*,整体温度等级 从医院 左连接选择hid AVU h作为总额定值 来自hid的hrating组 临时雇员 on hid=hospital.hid WHERE hospital.hid=:hid LIMIT 1