ThinkPHP learning notes (9) comprehensive query methods in Thinkphp

Source: Internet
Author: User
ThinkPHP study notes (9) comprehensive query methods in Thinkphp & lt ;? Php/*** common query ** 1. string format * 2. array form (recommended form) * $ user = M ('user'); * $ data ['username'] = 'user '; * $ list = $ user ThinkPHP study notes (9) comprehensive query methods in Thinkphp
 Where ($ data)-> select (); * 3. object format * $ user = M ('user'); * $ condition = new stdClass (); * $ condition-> username = 'user '; * $ list = $ user-> where ($ condition)-> select (); * judgment condition: (too many) query expression: 5.3. * eq (equal), ne (not equal), neq (not equal), gt (greater than), lt (less than), elt (less than or equal to), and * le (less than or equal), gte (greater than or equal to), ge (greater than or equal) * like * [not] between * [not] in * exp execute standard SQL statement * interval query * Combined query * composite query * Statistical query * locate query * SQL query * dynamic query **/class SelectAction extends Action {public fu Nction index () {$ user = M ('user'); // 1.1 array query (you can use a query expression to set the query range) /// DB_LIKE_FIELDS => 'username, password'; (fuzzy match is automatically performed) // $ data ['username'] = array ('like ', 'US % '); // $ data ['id'] = array ('between', '0, 10 '); // $ data ['id'] = array ('not between', array ('5', '10 ')); // $ data ['id'] = array ('in', '5, 8, 10 '); // $ list = $ user-> where ($ data) -> select (); // $ date ['id'] = array ('Exp ', 'In )'); // $ date ['createtime'] = array ('Exp ', 'create Time + 1'); // $ list = $ user-> where (array ('id' => 1)-> save ($ date ); // dump ($ list); // 1.2 object form // $ user = M ('user'); // $ condition = new stdClass (); // $ condition-> username = 'user'; // $ list = $ user-> where ($ condition)-> select (); // dump ($ list ); // 2 interval query // $ data ['id'] = array ('GT ', 3), array ('Lt', 10 )); // $ data ['id'] = array ('GT ', 3), array ('Lt', 10), 'or '); // $ data ['id'] = array ('GT ', 3), array ('Lt', 10), 'and'); // $ dat A ['username'] = array ('like', 'U % '), array ('like', 'a %'), 'OR '); // $ list = $ user-> where ($ data)-> select (); // dump ($ list); // 3. combined query // $ data ['username'] = 'user'; // $ data ['id'] = '8 '; // $ data ['_ logic'] = 'or'; // The condition is or // $ data ['_ string'] = 'Id <5 '; // add the standard SQL condition // $ list = $ user-> where ($ data)-> select (); // dump ($ list ); // 4. composite query (id <5 and (username = ''or createtim ='') // $ data ['username'] = array ('EQ ', 'User '); // $ da Ta ['createtime'] = array ('EQ ', '0'); // $ data [' _ logical '] =' or '; // The condition is or // $ where ['_ compute'] = $ data; // $ where ['id'] = array ('Lt', 2 ); // $ list = $ user-> where ($ where)-> select (); // dump ($ list); // 5. count \ max \ min \ avg \ sum // $ list = $ user-> where ('Id> 5')-> count ('id '); // dump ($ list); // $ list = $ user-> max ('createtime'); // dump ($ list ); // $ list = $ user-> min ('createtime'); // dump ($ list); // $ list = $ user-> avg ('createtime '); // Dump ($ list); // $ list = $ user-> sum ('createtime'); // dump ($ list); // 6. locate getN (); first (); last (); // 6.1 Initialization // $ user = new AdvModel ('user '); //// 6.1.2 or a new ComModel inherits AdvModel // $ user = new AdvModel ('user '); //// a record in the direct return result starts from 0 // $ list = $ user-> getN (0); // dump ($ list ); // 7. SQL query execute (update and write; affected rows have no result set), query (for query) // $ list = $ user-> query ('From tb_user order by id desc '); // dump ($ list); // 8. dynamic query (add the field name getBy after the method) Username or query top) $ user = new AdvModel ('user'); // $ list = $ user-> getByUsername ('user '); $ list = $ user-> top3 (); dump ($ list) ;}}?>

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.