SQL coherent operation [1]

Source: Internet
Author: User
Tags mail exchange

A Getting Started with coherence

Find the first two bits in the reverse order of the creation time in the ID 1,2,3,4.

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

1. Getting started with consistent operation

$user = M (' user '); Var_dump ($user->where (' ID in (1,2,3,4) ')->order ('date DESC')->limit (2)->select ());

The resulting SQL query statement is

PS: The Where, order, and limit methods here are coherent operations, so they all return to the $user itself,
You can swap positions. The Select method is not a coherent method and needs to be placed at the end to display the data set.

2. Array Manipulation

$user = M (' user '); Var_dump ($userarray(' where ' \ = ' id in (1,2,3,4) ', ' limit ' = ' 2 ', ' order ' = ' date DESC '));

or a

$user = M (' user '); Var_dump ($user->select (' where ' = = array(' id ' = =array(' Neq ', ' 1 ')), ' Order ' = ' date DESC ', ' limit ' = ' 2 '));    

Two Coherence approach

1.where
The Where method supports string conditions, array conditions (recommended usage), and multiple invocations

// string Mode $user = M (' user '); Var_dump ($user->where (' id=1 ')->select ());

Or

// indexed Array Mode $user = M (' user '); $map Var_dump ($user->where ($map)->select ());

Or

$user = M (' user '); $map [' id '] =  array(' eq ', 1); Var_dump ($user->where ($map)->where (' user= "crayon small New" ')->select ());

2.order
Order is used to sort the result set.

// Reverse $user = M (' user '); $map [' id '] =  array(' eq ', 1); Var_dump ($user// Positive-order default or ASC

Or

// Second Sort Var_dump ($user->order (' ID desc,email desc '),Select ()); PS: First by ID reverse, and then by email reverse

This usage in this database is meaningless, but if it is in the comparison of the time can be used, such as the ID for the language score, e-mail exchange for math results,

When the language score is the same time, then the comparison of mathematical results, this time only to use two sorting function.

Or

// array form prevents field from conflicting with MySQL keyword $user = M (' user '); $map [' id '] =  array(' eq ', 1); Var_dump ($userarray(' id ' = ' DESC ') ')->select ());

The use of arrays is the ability to add the ' symbol ' to the user field when SQL queries are available, which is more secure.

3.feild
The Feild method can return or manipulate fields that can be used for query and write operations.

Show ID and user two fields only
$user = M (' user '); Var_dump ($user->field (' id, user ')->select ());

Using SQL functions and aliases

$user = M (' user '); Var_dump ($user->field (' SUM (ID) as Nihao, user ')->select ());

Where the ' SUM (ID) ' of Nihao in Nihao is the set alias, which appears in the browser as

Using array parameters with SQL functions

$user = M (' user '); Var_dump ($userarray(' id ', ' Left (user,3) ' = ' left_user '))->select ());

Only the first three characters of the data in user are displayed

And the Left_user is also the alias of the set, in the SQL query statement is:

Get all fields

$user = M (' user '); Var_dump ($user

SQL coherent operation [1]

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.