SQL query Statements

Source: Internet
Author: User

I. Using strings as a conditional query

In Home/controller/usercontroller. Class. PHP under Insert

<? phpnamespace Home\controller;  Use Think\controller;  Use Think\model; class extends Controller {       publicfunction  model () {        $user = M (' user ' );         Var_dump ($user->where (' id=1 '),Select ())}           }

This is the most basic method of invocation, but more detailed is

Var_dump ($user

Second, the following describes the array Query method :

is to var_dump($user->where (' id=1 ')->select ()), this line is replaced with the following code

   $condition [' id ']=1;    $condition [' User ']= ' crayon small new ';    Var_dump ($user->where ($condition)->select ());    

What you see in the browser using the Debug tool is:

At this point, use where the default is ADN, if you want to change to or, and then add a statement:

$condition [' _logic ']= ' or ';

Third, the use of object query

Also var_dump($user->where (' id=1 ')->select ()); Replace with the following code:

$condition New \stdclass (); $condition->id=1; $condition->user = ' crayon small new ';

The Ps:stdclass class is a PHP built-in class that can be understood as an empty class, which can be understood here as a conditional
The field is saved as a member in the StdClass class. The "\" Here is to set the namespace to the root directory, otherwise it will
This class cannot be found for the current directory. Using objects and array queries, the effect is the same and can be interchanged. In most cases,
Thinkphp is recommended for more efficient use of array forms.

The following describes an expression query

For queries that want to implement fuzzy judgments, such as SQL queries that are greater than, equal to, or less than, you can use an expression query method.
Query expression format: $map [' field name '] = Array (' expression ', ' query condition ');

An expression query

An expression Meaning
EQ equals (=)
NEQ Not equal to (<>)
GT Greater than (>)
EGT Greater than or equal to (>=)
LT Less Than (<)
ELT Less than or equal (<=)
[NOT] Like Fuzzy query
[NOT] Between (not) Interval query
[NOT] Inch Not In query
BX7 expression queries, supporting SQL syntax

In Home/controller/usercontroller. Insert the following code in class. PHP to query:

$user = M (' user '); $map Array (' eq ', 1); Var_dump ($user->where ($map)->select ());

Query the id=1 data;

From EQ to elt simply change the EQ on the array (' EQ ', 1) to the line,

Here's how to like a fuzzy query:

$map[' user '] = array(' like ', '% small% '); Query the user name with "small" data,

$map[' user '] = array(' notlike ', '% small% '); Query the user name without the "small" data,

$map[' user '] = array(' like ', array('% small% ', '% wax% '), ' and '); Query the user name with "small" and "wax" data, then add and, if not added, the default is OR.

Here is the usage of between:

$map[' id '] = array(' between ', ' 1,3 '); Query ' ID ' for data between 1 and 3,

$map[' id '] = array(' between ',array(' 1 ', ' 3 ')); The same as the previous sentence,

$map[' id '] = array(' Not between ', ' 1,3 '); Query in addition to the ' ID ' is not equal to 1 to 3 of the data, then not and between with a space between the previous notlike no space between

Here is the usage of in:

$map[' id '] = array(' in ', ' 1,2,4 '); The query ID is 1,2,4 data;

$map[' id '] = array(' not ', ' 1,2,4 '); The query ID is not equal to the data in the one, and there are spaces between not and in

Exp: Custom: (PS: Use exp custom to write a where statement directly in the second parameter)

$map[' id ']=array(' exp ', ' =1 '); Data with Query ID equal to 1

$map[' id '] = array(' exp ', ' in (1,2,4) '); The query ID is 1,2,4 data;

$map [' id '] =  array(' exp ', ' =1 '); $map [' user '] =  array(' exp ', ' = ' crayon small new '); $map [' _logic '] = ' OR '; // WHERE is ((' id ' =1)) OR ((' user ' = "crayon Small New"))

SQL query Statements

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.