ThinkPHPwhere-php Tutorial

Source: Internet
Author: User
ThinkPHPwhere method detailed explanation classification: PHP Time: April 22, 2016

In the ThinkPHP framework, the usage of the where method is the essence of the query language and an important part of ThinkPHP's coherent operations. The where method supports strings, arrays, and objects. array query is very powerful and officially recommended. The where method can be used to perform queries including common queries, expression queries, quick queries, interval queries, and combined queries. The following PHP programmer Lei Xuesong will explain in detail the usage of the where method.

1. string query

M ("User")-> where ('status = 0')-> select (); // query records whose status is 0

2. array query

A. common query

$ Where ['status'] = 0; // The field is the subscript of the array, and the value is the corresponding condition M ("User ") -> where (array ('status' => '0')-> select (); // query records whose status is 0

Note: the subscript of the array must be a database field, otherwise it will be filtered.

B. expression query

Expression Description
EQ Equal to (=)
NEQ Not equal to (<>)
GT Greater than (>)
EGT Greater than or equal to (> =)
LT Less than (<)
ELT Less than or equal to (<=)
LIKE Fuzzy search
[NOT] (Not in) interval query
[NOT] IN (Not IN) IN query
EXP Expression query, supporting SQL syntax
$ Where ['status'] = array ("eq", 0); // The field is the subscript of the array, and the value is the corresponding condition M ("User ") -> where ($ where)-> select (); // query records whose status is 0
3. example of where method array query

1. how to use arrays to implement multiple conditions for the same field? For example, you can query records whose createdate value is greater than or equal to April 20, 2016 and less than or equal to April 23, 2016.

$ Where ['createdate'] = array ('egt', "2016-04-20"), array ('elt', "2016-04-23 ")); // use a two-dimensional array to solve multiple conditions of the same field M ("User")-> where ($ where)-> select (); // query records whose createdate value is greater than or equal to April 20, 2016 and less than or equal to April 23, 2016

2. use OR for array query? For example, query records whose status is equal to 0 or 1.

$ Where ['status'] = array ('EQ ', "0"), array ('EQ', "1"), "OR "); // use a two-dimensional array to solve multiple conditions of the same field M ("User")-> where ($ where)-> select (); // query records whose status is equal to 0 or 1

3. how many conditions have an array query with both and or? For example, query records whose createdate is less than or equal to April 20, 2016 and whose status is 1 or level is 0.

$ Where ['createdate'] = array ('egt', "2016-04-20"); $ where ['status'] = '1 '; $ condition ['_ lock'] = "AND"; $ map [' _ compute'] = $ where; $ map ['level'] = '0 '; $ map ['_ logic'] = 'OR'; M ("User")-> where ($ map)-> select (); // query records whose createdate is less than or equal to April 20, 2016 and whose status is 1 or level is 0

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.