我的 issue_points 表中有一個 jsonb 型別的位置列:
{
"geo": {
"lat": 57.994434,
"lng": 78.35716
},
"fias": {
"city": "some city fias code",
"region": "some region fias code"
},
"address": "some address"
}
使用 Laravel DB 外觀,我想使用 Haversine 公式找到相對于接收坐標的拾取點:
select * from "issue_points" where acos(
sin(radians(?))
* sin(radians(location->'geo'->'lat'))
cos(radians(?))
* cos(radians(location->'geo'->'lat'))
* cos(radians(?)- radians(location->'geo'->'lng'))
) * 6371 <= 20 order by "updated_at" desc
問題是,如果我想使用 location->'geo'->'lat' 獲取 lat 或 lng,則會引發以下 postgresql 例外:
"SQLSTATE[42883]: Undefined function: 7 ERROR: function radians(jsonb) does not exist\nLINE 3: * sin(radians(location->'geo'->'lat'))\n
^\nHINT: No function匹配給定的名稱和引數型別。您可能需要添加顯式型別轉換。(SQL: select * from "issue_points" where acos(\n sin(radians(55.03441))\n
- sin(radians(location->'geo'->'lat'))\n cos(radians(55.03441))\n * cos(radians(location->'geo'->'lat'))\n * cos(radians(73.46783)- radians(location->'geo'->'lng'))\n) * 6371 <= 20 order by "updated_at" desc)",
ps 如果您使用具有標量資料型別的原子欄位:lat (float) 和 lng (float),并嘗試使用以下代碼,則問題項將正確回傳:
select * from "issue_points" where acos(
sin(radians(?))
* sin(radians(lat))
cos(radians(?))
* cos(radians(lat))
* cos(radians(?)- radians(lng))
) * 6371 <= 20 order by "updated_at" desc
uj5u.com熱心網友回復:
每個運算子和函式都有一個型別。在->您使用它的情況下jsonb => jsonb- 它實際上不能是其他任何東西,您的結果可能是一個標量值、一個物件、一個陣列、一個包含...的物件陣列
所以 - 如果您確信您提取的值將始終具有您期望的型別,那么您可以這樣做cos(radians( (location->'geo'->'lat')::double precision )):
但是,如果您確實知道您的 json 始終是這樣的結構,那么就沒有理由讓它 json 存在嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515800.html
上一篇:檢查當前路線yii2
