复杂的SQL语句如何使用CDbCriteria来整合呢?
有这样一个SQL语句。是用来查询附近的人的。要知道有多少人使用。使用model的count方法。
下面是这个SQL语句:
SELECT userid ,username,homeland,headImage,(ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('.$lon.' * 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6378.137) as distance from user,refresh order by distance where geohash=:geohash1 or geohash=:geohash2 or geohash=:geohash3 or geohash=:geohash4 or geohash=:geohash5 or geohash=:geohash6 or geohash=:geohash7 or geohash=:geohash8 or geohash=:geohash9" limit 0,10;
其中User表是记录了user的信息。refresh表记录了的是用户随时经度和维度的变化。
$criteria = new CDbCriteria; $criteria->addCondition('where geohash=:geohash1 or geohash=:geohash2 or geohash=:geohash3 or geohash=:geohash4 or geohash=:geohash5 or geohash=:geohash6 or geohash=:geohash7 or geohash=:geohash8 or geohash=:geohash9'); $criteria->params=array(':current'=>$current); $criteria->order='distance DESC'; $refreshModel=Refresh::model(); $total=$planModel->count($criteria); $pager=new CPagination($total); $pager->pageSize=10; $pager->applyLimit($criteria); $planList= $planModel->findAll($criteria);
然后使用relation规则来得到User表的信息。
请问这样做能不能达到预期的效果?
最后好像生成的数组还要变一下,然后才能返回有效的信息(JSON)给客户端、但是这个where条件太坑爹了。能不能稍稍优化一下。
最关键的是:
(ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('.$lon.' * 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6378.137) as distance
这句话我该放在哪里?是放在select属性里面吗?
回复内容:
有这样一个SQL语句。是用来查询附近的人的。要知道有多少人使用。使用model的count方法。
下面是这个SQL语句:
SELECT userid ,username,homeland,headImage,(ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('.$lon.' * 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6378.137) as distance from user,refresh order by distance where geohash=:geohash1 or geohash=:geohash2 or geohash=:geohash3 or geohash=:geohash4 or geohash=:geohash5 or geohash=:geohash6 or geohash=:geohash7 or geohash=:geohash8 or geohash=:geohash9" limit 0,10;
其中User表是记录了user的信息。refresh表记录了的是用户随时经度和维度的变化。
$criteria = new CDbCriteria; $criteria->addCondition('where geohash=:geohash1 or geohash=:geohash2 or geohash=:geohash3 or geohash=:geohash4 or geohash=:geohash5 or geohash=:geohash6 or geohash=:geohash7 or geohash=:geohash8 or geohash=:geohash9'); $criteria->params=array(':current'=>$current); $criteria->order='distance DESC'; $refreshModel=Refresh::model(); $total=$planModel->count($criteria); $pager=new CPagination($total); $pager->pageSize=10; $pager->applyLimit($criteria); $planList= $planModel->findAll($criteria);
然后使用relation规则来得到User表的信息。
请问这样做能不能达到预期的效果?
最后好像生成的数组还要变一下,然后才能返回有效的信息(JSON)给客户端、但是这个where条件太坑爹了。能不能稍稍优化一下。
最关键的是:
(ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('.$lon.' * 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6378.137) as distance
这句话我该放在哪里?是放在select属性里面吗?

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

CakePHP 是 PHP 的開源框架。它旨在使應用程式的開發、部署和維護變得更加容易。 CakePHP 基於類似 MVC 的架構,功能強大且易於掌握。模型、視圖和控制器 gu

登入 CakePHP 是一項非常簡單的任務。您只需使用一項功能即可。您可以記錄任何後台程序(如 cronjob)的錯誤、異常、使用者活動、使用者採取的操作。在 CakePHP 中記錄資料很容易。提供了 log() 函數

Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫
