ThinkPHP data classification

【Foreword】

    For the classification display of data today, it is recommended to use the join method

 

【main body】

   Write multi-table join query in thinkphp3.2?

  First analyze the master table and slave table, the following is the definition

(1) The main table blog_article, from the table blog_cate
(2) Aliases: main table t1, slave table t2
(3) Association condition: t1.cateid=t2.id
Note: table is equivalent to join inline

 

(1) Native SQL writing:

select t1.*,t2.catename as catename from
blog_article as t1 left join blog_cate as t2 on t1.cateid=t2.id where t1.cateid=46;
(2) Thinkphp writing: I('id') is the received id
$model->field('t1.*,t2.catename as catename')
      ->alias('t1')
      ->join('left join blog_cate as t2 on t1.cateid=t2.id')
      ->where('t1.cateid = '.I('id'))
      ->select();

 

  If there are multiple tables, continue to add join after the table.

 

【Summarize】

      For 1-to-1, you can use the view model, but because multiple tables are associated together, and some of them are many-to-many, there is no way to use the association model. This 1-to-1 association query result screening problem occurs. I found a solution. The method is to use join, not an association model. If you join, you can filter the associated table fields!

 

Reference link:

http://document.thinkphp.cn/manual_3_2.html#join

 http://www.php.cn/code/26127.html

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326059021&siteId=291194637