ThinkPHP -- SQL query statement

Source: Internet
Author: User
: This article mainly introduces ThinkPHP-SQL query statements. For more information about PHP tutorials, see. I. query method

ThinkPHP provides three basic query methods: string condition query, index array condition query, and object bar query.
Component query. In most cases, we recommend that you use index arrays and objects as the query conditions, because they are more secure.
1. query using strings as conditions
// String as a condition query
$ User = M ('user ');
Var_dump ($ user-> where ('Id = 1 AND user = "Crayon" ')-> select ());
// The final generated SQL statement
SELECT * FROM 'think _ user' WHERE (id = 1 AND user = "Crayon ")
PS: You only need to include the conditions in the where query method. you can add AND other connectors to multiple conditions. We will
Learn more about SQL coherent operations.
2. use the index array as the query condition
// Index array as a condition query
$ User = M ('user ');
$ Condition ['id'] = 1;
$ Condition ['user'] = 'crayon Shinichi ';
Var_dump ($ user-> where ($ condition)-> select ());
// The final generated SQL statement
SELECT * FROM 'think _ user' WHERE ('id' = 1) AND ('user' = 'crayon small
New ')
PS: The default logical relationship of the index array query is AND. if you want to change to OR, you can use _ logic to define the query.
Query logic.
Add the following line based on the above code:
$ Condition ['_ logic'] = 'OR'; // change AND to OR by default.
3. query using objects
// Query objects as conditions
$ User = M ('user ');
$ Condition = new \ stdClass ();
$ Condition-> id = 1;
$ Condition-> user = 'crayon SHIN ';
Var_dump ($ user-> where ($ condition)-> select ());
// The final generated SQL statement
SELECT * FROM 'think _ user' WHERE ('id' = 1) AND ('user' = 'crayon small
New ')
PS: The stdClass class is a PHP built-in class, which can be understood as an empty class. here it can be understood as a condition
Fields are saved to the stdClass as members. The '\' here sets the namespace as the root directory. otherwise
This class cannot be found in the current directory. The query results of objects and arrays are the same and can be exchanged. In most cases,
ThinkPHP recommends using arrays for greater efficiency.

II. expression query

You can use tables for queries that require fuzzy judgment, such as SQL queries greater than, equal to, or less
Dashboard query method.
Query expression format: $ map ['Field name'] = array ('expression', 'query condition ');
Expression query table
Expression meaning
EQ equals (=)
NEQ is not equal to (<>)
GT greater than (>)
EGT greater than or equal to (> =)
LT is less than (<)
ELT less than or equal to (<=)
[NOT] LIKE fuzzy query
[NOT] BETWEEN (NOT) interval query
[NOT] IN (NOT) IN query
EXP expression query, supporting SQL syntax
PS: The expression is case insensitive.
// EQ: equal to (=)
$ Map ['id'] = array ('EQ ', 1); // where is id = 1
// NEQ: not equal to (<>)
$ Map ['id'] = array ('neq', 1); // where is id <> 1
// GT: greater than (>)
$ Map ['id'] = array ('GT ', 1); // where is id> 1
// EGT: greater than or equal to (> =)
$ Map ['id'] = array ('egt', 1); // where is id> = 1
// LT: less than (<)
$ Map ['id'] = array ('Lt ', 1); // where is id <1
// ELT: less than or equal to (<=)
$ Map ['id'] = array ('elt', 1); // where is id <= 1
// [NOT] LIKE: fuzzy query
$ Map ['user'] = array ('like', '% small %'); // where is like % small %
// [NOT] LIKE: fuzzy query
$ Map ['user'] = array ('notlike', '% small %'); // where is not like % small %
// [NOT] LIKE: fuzzy query array method
$ Map ['user'] = array ('like', array ('%', '% wax %'), 'AND ');
// Generated SQL
SELECT * FROM 'think _ user' WHERE ('user' LIKE '%' AND 'user'
LIKE '% wax % '))
// [NOT] BETWEEN: interval query
$ Map ['id'] = array ('between', '1, 3 ');
// Where is 'id' BETWEEN '1' AND '2'
// Same as above
$ Map ['id'] = array ('between', array ('1', '3 '));
// [NOT] BETWEEN: interval query
$ Map ['id'] = array ('not between', '1, 3 ');
// Where is 'id' not between '1' AND '2'
// [NOT] IN: interval query
$ Map ['id'] = array ('in', '1, 2, 4 ');
// Where is 'id' IN ('1', '2', '4 ')
// [NOT] IN: interval query
$ Map ['id'] = array ('not in', '1, 2, 4 ');
// Where is 'id' not in ('1', '2', '4 ')
// EXP: Custom
$ Map ['id'] = array ('Exp ', 'In (1, 2, 4 )');
// Where is 'id' not in ('1', '2', '4 ')
PS: use exp to customize where statements in the second parameter.
// EXP: add OR statement
$ Map ['id'] = array ('Exp ',' = 1 ');
$ Map ['user'] = array ('Exp ',' = "Crayon "');
$ Map ['_ logic'] = 'OR ';
// WHERE is ('id' = 1) OR ('user' = "Crayon "))

3. Quick Query

Quick query is a simplified method of multi-field query. multiple fields are separated by '|' to indicate OR, and '&' to indicate '&'
AND.
1. the same query conditions for different fields
// Use the same query conditions
$ User = M ('user ');
$ Map ['user | eemail'] = 'a'; // '| change' to '&' AND
Var_dump ($ user-> where ($ map)-> select ());
2. different query conditions for different fields
// Use different query conditions
$ User = M ('user ');
$ Map ['id & user'] = array (1, 'crayon SHIN', '_ multi' => true );
Var_dump ($ user-> where ($ map)-> select ());
PS: Set '_ multi' to true to allow the id to correspond to 1 and the user to correspond to 'crayon SHIN'. otherwise
If the id corresponds to 1 and corresponds to 'crayon SHIN. In addition, this setting should be placed at the end of the array.
// Supports using expressions in combination with quick query
$ User = M ('user ');
$ Map ['id & user'] = array ('GT ', 0), 'crayon update', '_ multi' => true );
Var_dump ($ user-> where ($ map)-> select ());

IV. interval query

ThinkPHP supports interval query for a field.
// Interval query
$ User = M ('user ');
$ Map ['id'] = array ('GT ', 1), array ('Lt', 4 ));
Var_dump ($ user-> where ($ map)-> select ());
// The third parameter setting logic OR
$ User = M ('user ');
$ Map ['id'] = array ('GT ', 1), array ('Lt', 4), 'OR ');
Var_dump ($ user-> where ($ map)-> select ());

5. combined query

Composite query is an extended query based on index array query.
Query (_ complex) and request string query (_ query). Because index arrays are used, duplicate queries are overwritten.
// String query (_ string)
$ User = M ('user ');
$ Map ['id'] = array ('EQ ', 1 );
$ Map ['_ string'] = 'User = "Crayon" AND email = "xiaoxin@163.com "';
Var_dump ($ user-> where ($ map)-> select ());
// Query the request string (_ query)
$ User = M ('user ');
$ Map ['id'] = array ('EQ ', 1 );
$ Map ['_ query'] = 'User = Crayon & email = xiaoxin@163.com & _ logic = OR ';
Var_dump ($ user-> where ($ map)-> select ());
PS: This method is URL without quotation marks.
// Composite query (_ complex)
$ User = M ('user ');
$ Where ['user'] = array ('like', '% ');
$ Where ['id'] = 1;
$ Where ['_ logic'] = 'OR ';
$ Map ['_ complex'] = $ where;
$ Map ['id'] = 3;
$ Map ['_ logic'] = 'OR ';
Var_dump ($ user-> where ($ map)-> select ());
PS: Composite queries can be used to construct more complex queries. here, id = 1 or id = 3 can be used to construct and implement these queries.

6. statistical query

ThinkPHP provides some data Statistics Query Methods.
// Total number of data entries
$ User = M ('user ');
Var_dump ($ user-> count ());
// The total number of fields. NULL is not counted.
$ User = M ('user ');
Var_dump ($ user-> count ('email '));
// Maximum value
$ User = M ('user ');
Var_dump ($ user-> max ('id '));
// Minimum value
$ User = M ('user ');
Var_dump ($ user-> min ('id '));
// Average value
$ User = M ('user ');
Var_dump ($ user-> avg ('id '));
// Sum
$ User = M ('user ');
Var_dump ($ user-> sum ('id '));

7. dynamic query

With the features of PHP5, ThinkPHP enables dynamic queries.
1. getBy dynamic query
// Find the data for email = xiaoin@163.com
$ User = M ('user ');
Var_dump ($ user-> getByemail ('xiaoxin @ 163.com '));
2. getFieldBy dynamic query
// Get the corresponding id value through user
$ User = M ('user ');
Var_dump ($ user-> getFieldByUser ('lufei ', 'id '));

8. SQL query

ThinkPHP supports native SQL queries.
1. query reading
// Query result set. if distributed read/write splitting is used, it is always executed on the read server.
$ User = M ('user ');
Var_dump ($ user-> query ('select * FROM think_user '));
2.exe cute write
// Update and write. if distributed read/write splitting is used, the data is always executed on the write server.
$ User = M ('user ');
Var_dump ($ user-> execute ('update think_user set user = "crayons" WHERE
Id = 1 '));
PS: Since subqueries use a lot of consistent operations, we will explain them in a consistent manner.

The above introduces ThinkPHP-SQL query statements, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.