table('think_blog blog, think_type type')"; 2. Use the Join method to query, with code such as "$Model ->join('work ON artist.id = work.artist_id')"."/> table('think_blog blog, think_type type')"; 2. Use the Join method to query, with code such as "$Model ->join('work ON artist.id = work.artist_id')".">

Home  >  Article  >  PHP Framework  >  How to query multiple data in thinkphp

How to query multiple data in thinkphp

藏色散人
藏色散人Original
2022-12-05 09:26:451950browse

How to query multiple data in thinkphp: 1. Use the Table method to query multiple tables, with syntax such as "$Model->table('think_blog blog, think_type type')"; 2. Use the Join method to query Query, code such as "$Model->join('work ON artist.id = work.artist_id')".

How to query multiple data in thinkphp

The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.

How to query multiple data in thinkphp?

THINKPHP Medium related query (multi-table query)

THINKPHP Medium related query (multi-table query) can use the table() method or the join method, Please see the example:

1. Table method: Define the name of the data table to be operated. You can dynamically change the name of the data table for the current operation. You need to write the full name of the data table, including the prefix. You can use an alias, for example:

$Model->Table('think_user user')
->where('status>1')
->select();
$Model->table('think_blog blog,think_type type')
->where('blog.typeid=type.id')
->field('blog.id as id,blog.title,blog.content,type.typename as type')
->order('blog.id desc' )
->limit(5)
->select();

The parameters of the Table method support strings and arrays. Usage in array mode:

$Model->Table(array('think_user'=>'user','think_group'=>'group'))
->where('status>1')
->select();

The advantage of using array mode definition is that it can avoid errors due to conflicts between table names and keywords.

Note: If the table method is not defined, the data table corresponding to or defined by the current model will be automatically obtained by default.

2. Join method: Query Join support. The parameters of the Join method support strings and arrays, and the join method is the only method that can be called multiple times in a coherent operation. For example:

$Model->join('work ON artist.id = work.artist_id')
->join('card ON artist.card_id = card.id')
->select();
//Left Join
$Model->table('user U')
->join('news N on U.id=N.cid')
->field('U.*,N.*')
->order('id desc')
->limit('8')
->findall();

The LEFT JOIN method is used by default. If you need to use other JOIN methods, you can change it to

$Model->join('RIGHT JOIN work ON artist.id = work.artist_id')
->select();
//Right Join
$Model->table('user U')
->join(array('right','news N on U.id=N.cid'))
->field('U.*,N.*')
->order('id desc')
->limit('8')
->findall();

If the parameters of the join method use arrays, you can only Use the join method once and cannot be mixed with the string method.

$Model->join(array(' work ON artist.id = work.artist_id', 'card ON artist.card_id = card.id'))
->select()

Using this coherent operation method can effectively improve the code clarity and development efficiency of data query.

Methods to view consecutively operated SQL statements:

echo $Model->getLastSql(); //打印一下SQL语句,查看一下

Example 2:

1, table()

$list = $user->table('user_status stats, user_profile profile')->where('stats.id = profile.typeid')->field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')->order('stats.id desc' )->select();

2.1 , join()2 table query

$user = new Model('user');
$list = $user->join('RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' );

2.2, join() multi-table query

        $list = $Form->join('think_sort ON think_form.sort_id = think_sort.sort_id' )->join('think_brand ON think_form.brand_id = think_brand.brand_id' )->select();

3, native query

$Model = new Model();
$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p->firstRow.','.$p->listRows;
$voList = $Model->query($sql);

Recommended learning: "thinkPHP video tutorial

The above is the detailed content of How to query multiple data in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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