2015-04-24 56 views
0

我有2個表,「位置」和「發生」。事件存儲事件,具有creatorId和時間戳。我試圖從位置表中爲發生時間戳5分鐘內的所有時間戳計算平均緯度,並將其放置在發生緯度列中。從mysql中的子查詢中的更新表訪問列表

我已經試過以下,並得到一個「未知列‘where子句’occurrence.creatorId「。

update occurrence set latitude = (
select avg(latitude) from (
select * from location where (
location.creatorId=occurrence.creatorId 
and location.timestamp<occurrence.timestamp+interval 5 minute 
and location.timestamp>occurrence.timestamp-interval 5 minute 
) 
) as test 
); 

我懷疑是我想寫這太像一個Java 。程序有人可以幫助我的大腦變得更加的MySQL指明分數

感謝 巴格

回答

0

試試這個:?!

update o 
set latitude = (select avg(latitude) 
       from location 
       where location.creatorId=o.creatorId 
       and location.timestamp<o.timestamp+interval 5 minute 
       and location.timestamp>o.timestamp-interval 5 minute) 
from occurrence o 

您正在收到錯誤「未知列」occurrence.creatorId'「,因爲表格發生無法訪問您最內層的子查詢。因此,從子查詢中引用表中所有對列的引用都是無效的。上面的查詢應該可以。

+0

工作就像一個魅力(雖然我去掉了「從發生○」因爲這是給我的悲傷「)。而在highsight,這是相當簡單的。這就是我得到的編碼過去我的就寢時間。謝謝。 – buggaby

0

試試這個。

update occurance o 
set o.latitude = (select avg(l.latitude) 
        from location l where o.creatorId=l.creatorId 
        and l.timestamp between o.timestamp-interval 5   
        minute and o.timestamp+interval 5 minute) 
+0

但是它看起來比上面的「between」函數要緊一些,謝謝! – buggaby

+0

編輯了答案,我用錯誤的表名「latitude」代替了「位置」 –