ThinkPHP3.2 SQL alias subquery

Source: Internet
Author: User

SELECT Info_key, Info_value, Info_status, Edit_time
F ROM (SELECT * from ' detail ' WHERE login = ' [e-mail protected] ' ORDER by Edit_time DESC) as AAA
GROUP by Info_key


Hope to realize the sub-query based on alias by Thinkphp, and finally refer to the following article to realize.

Http://www.thinkphp.cn/update/122.html


The following is the original text in the Help manual. (Search sub-query)


New subquery support has been added since version 3.0 and is available in two ways:

1, using the Select method when the parameter of the Select method is False, indicates that no query is returned only to build SQL, for example:

First construct the subquery sql $subQuery = $model->field (' Id,name ')->table (' tablename ')->group (' field ')->where ($where)- >order (' status ')->select (false);

When the Select method passes the false parameter, it means that the current query is not executed, but only the query SQL is generated.

2. Using the BuildSQL method

$subQuery = $model->field (' Id,name ')->table (' tablename ')->group (' field ')->where ($where)->order (' Status ')->buildsql ();

When the BuildSQL method is called, the actual query operation is not performed, but only the SQL statement that generated the query (in order to avoid confusion, the parentheses are added to the SQL), and then we call directly in subsequent queries.

Querying with subqueries $model->table ($subQuery. ' A ')->where ()->order ()->select ()

The constructed subquery SQL can be used for thinkphp's coherent operation methods, such as table where and so on.


ThinkPHP3.2 SQL alias subquery

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.