Thinkphp learning notes many table queries

Source: Internet
Author: User
In the process of learning thinkphp, You need to operate on multiple tables. But in the actual process, you always encounter various problems. Therefore, write this article as your learning process.

In the process of learning thinkphp, You need to operate on multiple tables. But in the actual process, you always encounter various problems. Therefore, write this article as your learning process.

During the operation, the two table queries are normal, but the three table queries start to cause problems.

There are three tables, including the pl table (uid, content), user table (id, username), and lyb table (uid, title)

You can use the following methods to query multiple tables:

(I) view model (recommended)

Define the view Model. You only need to inherit Think \ Model \ ViewModel and set the viewFields attribute.

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 the table contains fields with duplicate names, you can set the alias through =>, 'uid' => 'lid ');

View query:

View query is the same as query of different models.

$ Model = D ("pl")-> field ('uid, title, username, lyb_content ')-> select (); // pl indicates the Database Name

If duplicate data exists in the query results, you can use the group method to process the data.

(Ii) join

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

Join Operations usually have the following types. Different types of join operations affect the returned data results.

Inner join: If the table has at least one match, the returned row is equivalent to JOIN.
Left join: returns all rows from the LEFT table even if no match exists in the right table.
Right join: returns all rows from the RIGHT table even if no match exists in the left table.
Full join: if one of the tables matches, the row is returned.
The join method supports the following four types:

Perform operations on the preceding three tables.

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

(3) table

The table method is also one of the coherent operation methods of the model class. It is mainly used to specify the data table for the operation.

Usage

Generally, the system can automatically identify the corresponding data table when operating the model. Therefore, the table method is generally used:

The data table for the switchover operation;
Operate on multiple tables;

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

Note: The table method queries the values of all fields by default.

Articles you may be interested in:

  • Common Methods for Combined Query of multiple tables in ThinkPHP
  • 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.