ThinkPHP query language

Source: Internet
Author: User
Query Language in ThinkPHP: common query 1) string format $ list $ user-& gt; where (& amp; #39; usernamebbbb & amp; #39;)-& gt; select (); 2) array form $ data [& amp; #39; username & amp; #39;]...

Query Language in ThinkPHP:

Universal query

1) string format

$ List = $ user-> where ('username = bbbbbb')-> select ();

2) array form

  1. $ Data ['username'] = 'bbbbbb ';
  2. $ List = $ user-> where ($ data)-> select ();

3) object form

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

Query expression

EQ (=)

NEQ (! =)

GT (>) LT (<) [NOT] BETWEEN (corresponding to between IN SQL) IN

EGT (> =) ELT (<=) LIKE (corresponding to like in SQL)

EXP (complicated to use standard SQL statements)

Interval query

  1. $ Map ['id'] = array ('GT ', 3), array ('Lt', 10), 'OR ');
  2. $ Map ['name'] = array ('like', '% a %'), array ('like', '% B % '), array ('like', '% c %'), 'thinkphp', 'or ');

The corresponding query conditions are: ('name' LIKE '% a %') OR ('name' LIKE '% B %') OR ('name' LIKE '% c % ') OR ('name' = 'thinkphp ');

Combined query:

1) string mode query (_ string)

  1. $ User = M ('user ');
  2. $ Data [id] = array ('neq', 1 );
  3. $ Data ['username'] = 'aaaaa ';
  4. $ Data ['_ string'] = 'userpass = 123 and createtime = 100 ';
  5. $ List = $ user-> where ($ data)-> select ();

2) request string query method

  1. $ Data ['id'] = array ('GT ', 100 );
  2. $ Data ['_ query'] = 'userpass = 1 & username = aa & _ logic = or ';
  3. $ List = $ user-> where ($ data)-> select ();

3) composite query

  1. $ Wh ['username'] = array ('like', '% thinkphp % ');
  2. $ Wh ['userpass'] = array ('like', '3% ');
  3. $ Wh ['_ logic'] = 'or ';
  4. $ Data ['_ complex'] = $ wh;
  5. $ Data ['id'] = array ('GT ', 100 );
  6. $ List = $ user-> where ($ data)-> select ();

Corresponds to: (id> 100) AND (namelike '% thinkphp %') OR (titlelike '% thinkphp % '))

Statistics Query

  1. Count (): $ num = $ user-> count (); $ num = $ user-> count ('id ');
  2. Max ():
  3. Min ():
  4. Avg ():
  5. Sum ():

Locate query

The current model must inherit the advanced model class before it can be used.

  1. $ User-> where ('score> 0')-> order ('score desc')-> getN (2 );
  2. $ User-> where ('score> 80')-> order ('score desc')-> getN (-2 );
  3. $ User-> where ('score> 80')-> order ('score desc')-> first ();
  4. $ User-> where ('score> 80')-> order ('score desc')-> last ();

SQL query

  1. $ Model = new Model ();
  2. $ List = $ model-> query ("select * from think_user where id> 1 and id <10 ");
  3. $ Model = new Model ();
  4. $ Model-> execute ("update think_user set name = 'thinkphp' where status = 1 ");

Dynamic query

  1. $ User = $ User-> getByName ('liu21st ');
  2. $ User = $ User-> getFieldByName ('liu21st', 'id ');
  3. $ User-> where ('score> 80')-> order ('score desc')-> top5 ();

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.