Local or condition under laravel5.3-database operation and Global or condition (local and global for Orwhere)

Source: Internet
Author: User

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)

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.