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)