Multi-table query in ThinkPHP

Source: Internet
Author: User
Table method: defines 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, and use the alias.

1. Table method: defines 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, and use an alias, for example:

$ Model-> Table ('think _ user user ')

-> Where ('status> 1 ')

-> Select ();

$ Model-> table ('think _ 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. the usage of the array method is as follows:

$ Model-> Table (array ('think _ user' => 'user', 'think _ group' => 'Group '))

-> Where ('status> 1 ')

-> Select ();

The advantage of using arrays to define is to avoid errors due to table name and keyword conflicts.
Note: If the table method is not defined, the data table corresponding to or defined in the current model is automatically obtained by default.
2. Join method: query Join is supported. Join parameters support strings and arrays. join 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 want to use another JOIN method, you can change it

$ 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, the join method can only be used once and cannot be used together with the string method.

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

-> Select ()

This consistent operation method can effectively improve the code definition and development efficiency of data query.
Knowledge supplement:
How to View SQL statements of a coherent operation:

Echo $ Model-> getLastSql (); // Print the SQL statement and check it.

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.