2014-02-08 92 views
0

我正在嘗試以下操作。從另一個表中的特定行獲取一個表的平均值php

從名爲醫院的表中選擇id = hid的醫院。 從命名爲hrating

這裏另一個表中添加值「overall_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"} 

我在哪裏錯了。

回答

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 
0

試試這個:

SELECT *醫院,temp.overall_rating 從醫院 LEFT JOIN(SELECT藏AVG(rating_h)由HID overall_rating FROM hrating組)對HID溫度
= hospital.hid WHERE hospital.hid =:hid LIMIT 1

相關問題