Link query instance _ php instance in ThinkPHP

Source: Internet
Author: User
This article mainly introduces the usage of associated queries in ThinkPHP. The example analyzes the usage of table, join, native query, and multi-table query in ThinkPHP, which has some reference value, if you need it, refer to the example in this article to describe the usage of join queries in ThinkPHP. Share it with you for your reference. The specific analysis is as follows:

You can use the table () method or join method for join queries (Multi-table queries) in THINKPHP, as shown in the following example:

1. table ()

The Code is as follows:

$ 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. join ()

The Code is as follows:

$ User = new Model ('user ');
$ List = $ user-> join ('Right JOIN user_profile ON user_stats.id = user_profile.typeid')-> select ();


3. Native Query

The Code is as follows:

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


4. Multi-Table query

The Code is as follows:

$ Model-> field ('user. name, role. title ')-> table ('Think _ user, think_role role')-> limit (10)-> select ();


Or:

The Code is as follows:

$ Model-> field ('user. name, role. title ')-> table (array ('Think _ user' => 'user', 'Think _ role' => 'role')-> limit (10) -> select ();

I hope this article will help you design PHP programs based on the ThinkPHP framework.

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.