Thinkphp3.2.3

Source: Internet
Author: User
This sentence is changed to a coherent operation, which is very difficult for new users. It is difficult for new users to switch from the {code...} sentence to a coherent operation.

SELECT * FROM `hx_shop_period` WHERE ( id in ( SELECT DISTINCT  `pid` FROM `hx_shop_record` WHERE ( uid=101600 ) ORDER BY create_time desc  ) ) ORDER BY state asc,end_time desc LIMIT 0,20

Reply content:

This sentence is replaced by a coherent operation, which is difficult for new users.

SELECT * FROM `hx_shop_period` WHERE ( id in ( SELECT DISTINCT  `pid` FROM `hx_shop_record` WHERE ( uid=101600 ) ORDER BY create_time desc  ) ) ORDER BY state asc,end_time desc LIMIT 0,20

// Obtain an SQL statement without executing $ sub_query = M ()-> table ('hx _ shop_record ')-> distinct (true)-> field ('pid ') -> where (['uid' => 101600])-> order ('create _ time DESC ')-> buildSql (); // use a subquery as a condition, obtain the result $ rst = M ()-> table ('hx _ shop_period ')-> where ('Id in '. $ sub_query)-> order ('State ASC, end_time DESC ')-> limit (0, 20)-> select ();

In the mainstream database operation class libraries, there will be consistent operation functions, which is not troublesome, but very useful. The benefits include:
1. Do not remember the order of SQL substatements
2. Avoid injection risks
3. Some common methods are encapsulated, which are easy to use.
4. Assemble and execute multiple times to organize complex statements.

$ Res = $ model-> field ('pid ')-> where ('uid = 101600')-> order ('create _ time desc ') -> select (false );
$ Where ['id'] = array ('in', $ res)
$ This-> where ($ where)-> order ('State asc, end_time desc ')-> limit (0, 20)-> select ();

Well, in fact, I have never encountered this kind of problem. I wrote this document based on experience.
I recommend that you use $ Model-> query (native SQL)

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.