Yii2 the query where parameter of the search and deletion to change the _php instance

Source: Internet
Author: User

Overview

Because the Official handbook about where the introduction is relatively small, so want to organize their own, so that everyone's learning and their own back to query. This article details how and, or, between, in, like, in the Where method and examples are used.

and

We want to query for data with IDs greater than 1 and less than 3
$userInfo = User::find ()->where ([' and ', ' ID > 1 ', ' ID < 3 '])->all ();
Or in the following ways, more secure
$userInfo = User::find ()->where ([' and ', [' > ', ' id ', 1], [' < ', ' ID ', 3]])->all (); 
   
    //often we deal with more complex SQL
//If we want to query name is Harry and ID greater than 1 or ID is less than 3
$userInfo = User::find ()->where (
[' and ', 
[' = ', ' name ', ' Harry '], 
[' or ', 
[' = ', ' id ', 1], 
[' = ', ' ID ', 3]
]]
->asarray ()->all ();
Note: the Asarray () method displays the data as an array
   

Or

We want to query the data with ID equal to 1 or ID equal to 3
$userInfo = User::find ()->where ([' or ', ' id = 1 ', ' id = 3 '])->all ();
We can also use the following methods
$userInfo = User::find ()->where ([' or ', [' = ', ' id ', 1], [' = ', ' ID ', 3]])->all ();
Suppose we want to query ID within 4,8,9 range or ID within 1,2,3 range?
$userInfo = User::find ()->where ([' or ', [' id ' => [4,8,9]], [' ID ' => [1,2,3]]])->all ();

Between

We want to query ID within 1 to 10 of the range
$userInfo = User::find ()->where ([' Between ', ' id ', 1, ten])->all ();

In

We want to query ID in the range of 1, 2, 3
$userInfo = User::find ()->where ([' In ', ' id ', [1,2,3]])->all ();

Like

We want to query the data that contains the character "Zhang" in name
$userInfo = User::find ()->where ([' Like ', ' name ', '->all ']) ();
If we want to match the name to include "Zhang" this character, but also have to include the "three" this character
$userInfo = User::find ()->where ([' Like ', ' name ', ['], ' C ']])->all ( );
If we only need to pass the left can
$userInfo = User::find ()->where ([' Like ', ' name ', '% three ', false])->all ();
So the right side is the same.

The above is a small set to introduce Yii2 additions and deletions to check the query where the parameters of the relevant knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.