Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Operation method of thinkphp Multi-table query

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "thinkphp multi-table query operation method", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "thinkphp multi-table query operation method"!

In the course of operation, there is no problem with the two table queries, but the three table queries begin to have problems.

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

There are several methods for multi-table query operations:

㈠ View Model (recommended)

To define a view model, just inherit Think\ Model\ ViewModel, and then set the viewFields property

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 with duplicate names in the table, you can set the alias by = > 'uid'= >' lid')

View query:

View queries are the same as queries for different models.

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

If you find duplicate data in the results of the query, you can also use the group method to process it.

㈡ join

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

There are usually several types of join, and different types of join operations affect the results of the data returned.

INNER JOIN: if there is at least one match in the table, a row is returned, which is equivalent to JOIN

LEFT JOIN: returns all rows from the left table even if there is no match in the right table

RIGHT JOIN: returns all rows from the right table even if there is no match in the left table

FULL JOIN: returns a row as long as there is a match in one of the tables

The join method can support the above four types:

Also operate on the above 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 ()

㈢ table

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

Usage

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

Data table for switching operation

Operate on multiple tables

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

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

Thank you for your reading, the above is the content of the operation method of thinkphp multi-table query, after the study of this article, I believe you have a deeper understanding of the operation method of thinkphp multi-table query, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report