我正在尝试执行以下操作。
从名为“医院”的表中选择 id = hid 的医院。 向其中添加值“overall_ rating”并获取所有评级,并从另一个名为 h rating 的表中获取其平均值
这是我的查询
$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"}
我哪里错了。
最佳答案
您的查询中似乎没有“FROM Hospital”位?
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
关于php - 获取另一个表中特定行的一个表的平均值 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21642878/