thinkphp Study notes Multi-table query, thinkphp study notes _php Tutorial

Source: Internet
Author: User

thinkphp study notes, thinkphp study notes


There is no problem with both table queries during the operation, but the three-table query begins to have problems

With the following three tables, the table is PL table (uid,content), User table (id,username), Lyb table (Uid,title)

There are several ways to do a multi-table query:

㈠ view model (recommended)

To define the view model, simply inherit the Think\model\viewmodel and set the ViewFields property to

Public $viewFields = Array ('    pl '    =>array (' uid ', ' rid ', ' content '),    ' user '    =>array (' id ', ' Username ', ' _on ' = ' pl.uid=user.id '),    ' Lyb '    =>array (' uid ' = ' lid ', ' content ' = ' lyb_content ', ' Title ', ' _on ' = ' pl.uid=lyb.uid '),//If there is a field name in the table, you can set the alias by = =, ' uid ' = ' lid '    );   

View query:

There is no difference between a view query and a query of a different model.

$Model = D ("pl")->field (' Uid,title,username,lyb_content ')->select ();  PL is the database name

You can also use the group method if you find that the result of the query has duplicate data.

㈡join

The Join method is also one of the coherent operations methods used to query data from these tables based on the relationship between the columns in two or more tables.

Joins typically have the following types, and different types of join operations affect the returned data results.

INNER join: Returns a row if there is at least one match in the table, equivalent to join
Left JOIN: Returns all rows from the table, even if there is no match in the right table
Right JOIN: Returns all rows from the correct table even if there is no match in the left table
Full JOIN: Returns a row if there is a match in one of the tables
The Join method can support four of the above types:

The same is true for the above three sheets

$Model = D ("pl")->join (' lyb on pl.uid = Lyb.uid ')->join (' user on pl.uid = User.ID ')-& Gt;field (' user.username,lyb.title,pl.content ')->select ();

㈢table

The table method is also one of the consistent operations of the model class and is used primarily to specify the data table for the operation.

Usage

In general, the system can automatically recognize the current corresponding data table when manipulating the model, so the table method is usually used to:

Data table for switching operations;
operate on multiple tables;

$Model = D ("pl")->field (' Pl.content,user.username,lyb.title ')->table (' Pl,lyb,user ')->limit (Ten) Select ();

Note: The table method defaults to querying the values of all fields


thinkphp Multi-Table query problem

Two ways:
Multi-Table query:

$list =m ()->table (Array (' think_select ' = ' this0 ', ' think_student ' = ' this1 ', ' think_class ' = ' this2 '))
->where (' This0.stu_id=this1.id and This0.class_id=this2.id ')
->field (' this0.id this0_id,this1.id this1_id,this2.id this2_id ')->select ();
Generate SQL:
Select This0.id this0_id,this1.id this1_id,this2.id this2_id

From Think_select this0,think_student This1,think_class This2
where This0.stu_id=this1.id and This0.class_id=this2.id

Link query:

$list =m ()->table ("Think_select this0")->join (' Think_student this1 on This0.stu_id=this1.id ')
->join (' Think_class this2 on This0.stu_id=this2.id ')
->field (' this0.id this0_id,this1.id this1_id,this2.id this2_id ')->select ();
Generate SQL:
Select This0.id this0_id,this1.id this1_id,this2.id this2_id

From Think_select This0 left join Think_student this1 on This0.stu_id=this1.id
Left join Think_class This2 on This0.stu_id=this2.id

thinkphp Multi-table query how to write?


as follows!--? php
class Articleviewmodel extends viewmodel{
Public $viewFields = Arra Y (
' article ' = = Array (' id ', ' title ', ' content ', ' key_id ', ' task_id ', ' aout ', ' re ', ' otime ', ' url ', ' Correlation ', ' C _length ', ' ad ', ' _type ' = ' left '),
' task ' = = Array (' _on ' = ' article.task_id=task.id ', ' Task ', ' num ', ' able ') , ' user_id ', ' Lang ', ' _type ' = ' left '),
' user ' = = Array (' _on ' = ' task.user_id=user.id ', ' Manage ', ' alie ', ' Belong_to ', ' _type ' = ' left '),
);
}
,
specifically refer to thinkphp Manual view model section
 

http://www.bkjia.com/PHPjc/851350.html www.bkjia.com true http://www.bkjia.com/PHPjc/851350.html techarticle thinkphp Study notes of multiple table query, thinkphp Learning notes in the operation process, two tables query no problem, but three table query began to appear the problem has the following three tables, the table is ...

  • 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.