我正在努力做到以下。
从名为hospital的表中选择id = hid的医院。 为它添加一个值“overall_rating”并获取所有评级并从另一个名为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"}
我错在哪里。
答案 0 :(得分:2)
您的查询中似乎没有“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
答案 1 :(得分:0)
试试这个:
选择医院。*,temp.overall_rating
来自医院
LEFT JOIN(选择隐藏AVG(rating_h)作为overall_rating
来自hrating的hrating组
)临时
在hid = hospital.hid
在hospital.hid =:hid LIMIT 1