ThinkPHP query language

Source: Internet
Author: User
1. general queries include the where condition in queries. there are at least three forms of EQNEQ! GT & gt; EGT & gt; LT & lt;/div & gt; ELT & lt; LIKElike [NOT] BETWEEN [not] between [not] in is recommended to use arrays...

1. common query

There are at least three forms of where conditions for a query.

  1. EQ =
  2. NEQ! =
  3. GT>
  4. EGT> =
  5. LT < 
  6. ELT <=
  7. LIKE like
  8. [NOT] BETWEEN [not]
  9. [Not] in

Array query is recommended.

1. string format:

'Id> 5 and id <9'

2. array form

  1. $ User = M ('user ');
  2. $ Data ['username'] = 'liwenkai ';
  3. $ List = $ user-> where (array ('username' => 'liwenkai')-> select ();
  4. $ List = $ user-> where ($ data)-> select ();
 

3. object form

  1. $ User = M ('user ');
  2. $ A = new stdClass ();
  3. $ A-> username = 'liwenkai ';
  4. $ List = $ user-> where ($ a)-> select ();

4. query expression

  1. Equal to EQ
  2. NEQ is not equal
  3. GT>
  4. EGT greater than or equal
  5. LT is less
  6. ELT less than or equal
  7. LIKE is equivalent to like in SQL
  8. [NOT] BETWEEN Query Interval
  9. [NOT] IN query set
  10. EXP refers to the use of standard SQL statements for more complex scenarios.
  11.  
  12. $ Data ['Field name'] = array ('expression ', 'query condition ');
  13. $ Data ['liwenkai'] = 'liwenkai'; actually, it refers to $ data ['liwenkai'] = array ('EQ ', 'liwenkai ');
  14. $ Data ['username'] = array ('like', 'Peng % ');
  15. $ List = $ user-> where ($ data)-> select ();

II. interval query

  1. $ User = M ('user ');
  2. $ Data ['id'] = array ('GT ', 20), array ('Lt', 23), 'and ');
  3. $ List = $ user-> where ($ data)-> select ();
  4. Dump ($ list );
  5. $ Data ['username'] = array ('like', 'p % '), array ('like', 'H %'), 'OR ');

III. combined query

  1. $ User = M ('user ');
  2. $ Data ['username'] = 'pengyanjie ';
  3. $ Data ['password'] = array ('EQ ', 'pengyanjie ');
  4. $ Data ['id'] = array ('Lt ', 30 );
  5. $ Data ['_ logic'] = 'or ';
  6. $ List = $ user-> where ($ data)-> select ();
  7. Dump ($ list );

IV. Composite query

  1. $ User = M ('user ');
  2. $ Data ['username'] = array ('EQ ', 'pengyanjie ');
  3. $ Data ['password'] = array ('like', 'p % ');
  4. $ Data ['_ logic'] = 'or ';
  5. $ Where ['_ complex'] = $ where;
  6. $ Where ['id'] = array ('Lt ', 30 );
  7. $ List = $ user-> where ($ data)-> select ();
  8. Dump ($ list );
  9. // Equivalent to (id <30) and (username = pengyanjie) or (password like p % ))

5. statistical query

  1. Echo $ user-> count ();
  2. Echo'
    ';
  3. Echo $ user-> max ('id ');
  4. Echo'
    ';
  5. Echo $ user-> where ('Id <30')-> min ('id ');
  6. Echo'
    ';
  7. Echo $ user-> avg ('id ');
  8. Echo'
    ';
  9. Echo $ user-> sum ('id ');

6. locate and query

  1. $ User = new AdvModel ('user'); // instantiate the advanced model AdvModel
  2. // $ User = M ('user', 'commonmodel'); // you can use the CommonModel to inherit the AdvModel.
  3. $ List = $ user-> order ('Id desc')-> getN (2); // The third entry in the returned result
  4. Dump ($ list );
  5.  
  6. $ List = $ user-> order ('Id desc')-> last (); // return the last entry
  7. $ List = $ user-> order ('Id desc')-> first (); // return the first entry

VII. SQL query

Excute () is mainly used for updating and writing data.

  1. $ Model = new Model () // instantiate a model object without any data tables
  2. $ Model-> execute ("update think_user set name = 'thinkphp' where status = 1 ");

Query () is mainly used for query

  1. $ User = M ();
  2. $ List = $ user-> query ('select * from aoli_user order by id desc ');
  3. Dump ($ list );

8. dynamic query

  1. $ User = M ('user ');
  2. $ List = $ user-> getByusername ('pengyanjie ');
  3. $ List = $ user-> getByusername ('pengyanjie ');
  4. Dump ($ list );
  5.  
  6. $ User = new AdvModel ('user ');
  7. $ List = $ user-> top5 (); // The first five
  8. 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.