复杂的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 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック

PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP でデータベースを操作するのは非常に簡単です。この章では、CRUD (作成、読み取り、更新、削除) 操作について理解します。

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP へのログインは非常に簡単な作業です。使用する関数は 1 つだけです。 cronjob などのバックグラウンド プロセスのエラー、例外、ユーザー アクティビティ、ユーザーが実行したアクションをログに記録できます。 CakePHP でのデータのログ記録は簡単です。 log()関数が提供されています

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、
