When the user name is not empty
SELECT * from ' account_recharge ' left join ' order ' on ' account_recharge '. ' OrderNo ' = ' order '. ' OrderNo ' Left JOIN ' user_account ' on ' ORDER '. ' userid ' = ' user_account '. ' userid ' where (' User_account '. ' UserName ') Like '%18% ' or (' user_account ', ' mobileno ' like '%18% ') or (' User_account '. ' E-mail ' like '%18% ')) and ' Account_recharge '. ' Ordertime ' between ' 2017-08-11 0:0:0 ' and ' 2017-08-11 23:59:59 ' ORDER by em> ' account_recharge '. ' Ordertime ' DESC;
One of the above SQL statements in laravel5.3:
$model = db::connection (' Order_user ')->table (' Account_recharge ')
->leftjoin (' ORDER ', ' Account_recharge. OrderNo ', ' = ', ' ORDER. OrderNo ')
->leftjoin (' User_account ', ' ORDER. UserId ', ' = ', ' User_account. UserId ')
->select (' account_recharge.* ', ' User_account. UserName ', ' User_account. Mobileno ', ' User_account. EMail ')
->orderby (' Account_recharge. Ordertime ', ' DESC ');
if (!empty ($username)) {
$username = Trim ($username);
$model = $model->where (function ($query) use ($username) {
$query->where (' User_account. UserName ', ' like ', "% $username%")
->orwhere (function ($query) use ($username) {
$query->where (' User_account. Mobileno ', ' like ', '% $username% ');
});
->orwhere (function ($query) use ($username) {
$query->where (' User_account. EMail ', ' like ', '% $username% ');
});
});
}
Note: In the judgment that the username is not empty, the undefined variable is reported if no use is used username
Contrast
First Kind
$model = $model->where (function ($query) use ($username) {
$query->where (' User_account. UserName ', ' like ', "% $username%")
->orwhere (function ($query) use ($username) {
$query->where (' User_account. Mobileno ', ' like ', '% $username% ');
});
->orwhere (function ($query) use ($username) {
$query->where (' User_account. EMail ', ' like ', '% $username% ');
});
});
}
And
The second Kind
$model = $model >where (' User_account. UserName ', ' like ', '% $username% ')->orwhere (' Mobileno ', ' like ', '% $username% ')->orwhere
(' EMail ', ' like ', '% $username% ');
}
The difference
The first is the local or condition, which is just the or judgment of the username, and the relationship with the other conditions, and the SQL statement that is just starting to appear
The second is the global or condition, if the second statement is replaced by the first statement, the equivalent of the SQL statement
SELECT * from ' account_recharge ' left join ' order ' on ' account_recharge '. ' OrderNo ' = ' order '. ' OrderNo ' Left JOIN ' user_account ' on ' ORDER '. ' userid ' = ' user_account '. ' userid ' where ' user_account '. ' UserName ' Like '%18% ' or (' user_account ', ' mobileno ' like '%18% ') or (' User_account '. ' E-mail ' like '%18% ') and ' Account_recharge '. ' Ordertime ' between ' 2017-08-11 0:0:0 ' and ' 2017-08-11 23:59:59 ' ORDER by em> ' account_recharge '. ' Ordertime ' DESC;
At this point, you will find that as long as the username exists, your other conditions will expire.
This is the pit I skipped ...
Local or condition under laravel5.3-database operation and Global or condition (local and global for Orwhere)