Details of ThinkPHP5 Union (association) query, multi-condition query and aggregation query instances, and thinkphp5 instances

Source: Internet
Author: User

Details of ThinkPHP5 Union (association) query, multi-condition query and aggregation query instances, and thinkphp5 instances

This example describes ThinkPHP5 Union (association) queries, multi-condition queries, and aggregate queries. We will share this with you for your reference. The details are as follows:

1. Join (join) Query

1. Project table

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`));

2. Version Number table

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`));

3. Joint Query

$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();

Ii. Multi-condition Query

Method 1:

Put the query conditions in the array as the where function parameter, but if there is a condition parameter greater than or less than this, the array cannot be assigned a value.

Example 1:

$where=array(  "version_name"=>$version_name,  "project_name"=>$project_name);$userdata=$this->where($where)->find();

Example 2:

$where=array(   "version_name"=>$version_name,   "project_name"=>$project_name);$userdata=$this->where($where)->select();

Example 3:

$where=array(  "version_id"=>$version_id);$version_name = model("Version")->where($where)->field("version_name")->find();

Method 2:

Multiple SQL query statements are used as the where parameter. Therefore, a condition greater than or below is supported.

$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();

Method 3:

Place SQL query statements in multiple where functions

$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();

3. Aggregate max Functions

1. The newly inserted update package can be returned as follows, but only the create_time field will be returned. The entire record field cannot be returned.

$package = model('admin/Package')->where("project_id=".$Project_version['project_id'])->where("version_id=".$Project_version['version_id'])->where("status>1")->max(create_time)

2. You can use the following to find the latest inserted record and return the entire record field. First, sort order and then find the first record.

$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();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.