Home > PHP Framework > ThinkPHP > body text

About ThinkPHP's join query not using the default table prefix

藏色散人
Release: 2021-04-30 09:26:21
forward
2643 people have browsed it

The following tutorial column will introduce to you how ThinkPHP's join query does not use the default table prefix. I hope it will be helpful to friends in need! ThinkPHP’s join related query does not use the default table prefix

Regarding ThinkPHP’s related query, the official document describes it like this:

The above join function requires three parameters, which are:
About ThinkPHPs join query not using the default table prefixjoin

The (complete) table name and alias to be associated, supports three writing methods:

写法1:[ '完整表名或者子查询'=>'别名' ]
写法2:'完整表名 别名'
写法3:'不带数据表前缀的表名'
Copy after login

condition

关联条件,可以为字符串或数组, 为数组时每一个元素都是一个关联条件。
Copy after login

type

关联类型,可以为: INNER、LEFT、RIGHT、FULL,不区分大小写,默认为INNER。
Copy after login

Different prefixes

Generally, they are queried in the same database. In this case, the

same table is used by default Prefix, such as (shop_)

, so when using

database model

associated query is often written like this:

Order::alias('o')
     ->join('user u', 'o.user_id = u.id')
     ->select();
Copy after login
In the above code, because it is using model query, so By default, the table prefix will be added. The complete table names of the two tables are shop_order and

shop_user

, and the association type defaults to INNER association. But if you associate a table with a different prefix (for example: pay_record) , the above query statement will obviously not work. In this case, you need to slightly modify the association statement. , the modified code is as follows:

Order::alias('o')
     ->join(['pay_record' => 'r'], 'o.pay_id = r.id')
     ->select();
Copy after login

In this way, the model can be used to associate tables with different prefixes for query. Summary

The above method is actually

modifying the join parameter in the join function from a string to an array

; ThinkPHP is an excellent development framework. The above The association method is only one of them. For more methods, please refer to the official manual: ThinkPHP official manual.

Related recommendations:
The latest 10 thinkphp video tutorials

The above is the detailed content of About ThinkPHP's join query not using the default table prefix. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template