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!