這篇文章主要介紹了ThinkPHP5聯合(關聯)查詢、多條件查詢與聚合查詢,結合實例形式總結分析了thinkPHP5常用查詢操作技巧,需要的朋友可以參考下
本文實例講述了ThinkPHP5聯合(關聯)查詢、多條件查詢與聚合查詢。分享給大家供大家參考,具體如下:
一、聯合(關聯)查詢
##1. 項目表DROP TABLE IF EXISTS `darling_project`; CREATE TABLE `darling_project` ( `project_id` int(32) NOT NULL AUTO_INCREMENT, `project_name` varchar(20) NOT NULL, `create_time` int(32) NOT NULL, PRIMARY KEY (`project_id`), UNIQUE KEY `project_id` (`project_id`), UNIQUE KEY `project_name` (`project_name`) );
DROP TABLE IF EXISTS `darling_version`; CREATE TABLE `darling_version` ( `version_id` int(32) NOT NULL AUTO_INCREMENT, `project_id` int(32) NOT NULL, `version_name` varchar(128) NOT NULL, `create_time` int(32) NOT NULL, PRIMARY KEY (version_id), UNIQUE KEY `version_id` (`version_id`) );
$where=array( "version_id"=>$_POST['version_id'] ); $Project_version = model('Project')->join("darling_version","darling_version.project_id = darling_project.project_id")->where($where)->find();
二、多條件查詢
#方法一:把查詢條件放到陣列裡作為where函數參數,但是如果有大於小於這樣的條件參數,則陣列裡是無法賦值的。 範例1:$where=array( "version_name"=>$version_name, "project_name"=>$project_name ); $userdata=$this->where($where)->find();
$where=array( "version_name"=>$version_name, "project_name"=>$project_name ); $userdata=$this->where($where)->select();
$where=array( "version_id"=>$version_id ); $version_name = model("Version")->where($where)->field("version_name")->find();
$package = model('admin/Package') ->where("project_id=".$Project_version['project_id']." and version_id=".$Project_version['version_id']." and status>1") ->order('create_time desc') ->find();
把SQL查詢語句放到多個where函數裡
$package = model('admin/Package') ->where("project_id=".$Project_version['project_id']) ->where("version_id=".$Project_version['version_id']) ->where("status>1") ->order('create_time desc') ->find();
$package = model('admin/Package') ->where("project_id=".$Project_version['project_id']) ->where("version_id=".$Project_version['version_id']) ->where("status>1")->max(create_time)
$package = model('admin/Package') ->where("project_id=".$Project_version['project_id']) ->where("version_id=".$Project_version['version_id']) ->where("status>1") ->order('create_time desc') ->find();
ThinkPHP框架分散式資料庫連接方法詳解
thinkPHP簡單實作多個子查詢語句的方法
########### ###########以上是ThinkPHP5聯合(關聯)查詢、多條件查詢與聚合查詢實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!