在MySQL中组合了几个WHERE子句

时间:2017-02-26 15:14:24

标签: php mysql where-clause

我有以下php代码可以正常工作:

$sql="UPDATE `fa` SET `group` = '27',`locked` = '1'  WHERE `send` LIKE '%Amazon%' AND `Locked` =0";
$result=mysqli_query($conn,$sql);
$sql="UPDATE `fa` SET `group` = '27',`locked` = '1'  WHERE `send` LIKE '%ebay%' AND `Locked` =0";
$result=mysqli_query($conn,$sql);
$sql="UPDATE `fa` SET `group` = '27',`locked` = '1'  WHERE `send` LIKE '%google%' AND `Locked` =0";
$result=mysqli_query($conn,$sql);

有没有办法把这些线放到一行? 只是为了清理代码。

2 个答案:

答案 0 :(得分:4)

您可以使用OR

update `fa`
set `group` = '27',
    `locked` = '1'
where (
        `send` like '%Amazon%'
        or `send` like '%ebay%'
        or `send` like '%google%'
        )
    and `Locked` = 0

答案 1 :(得分:2)

您可以同时使用ORAND

WHERE (`send` LIKE '%Amazon%' OR `send` LIKE '%ebay%' OR `send` LIKE '%google%') AND `Locked` =0

了解:Combining the AND and OR Conditions