在mysql where子句中使用多个括号

时间:2015-05-18 20:34:29

标签: mysql

这是我想要在“DELETE”声明中做的事情:

delete from table1 
where (username = "Jonathan" or username = "Amy") 
and (field2 = "Jonathan" or field3 = "Jonathan" or field4 = "Jonathan" or field2 = "Amy" or field 3 = "Amy" or field4 = "Amy")

当然有办法做到这一点吗?

编辑: 这是我正在使用的实际代码。我一定是做错了什么:

$sql = "DELETE FROM friend_requests
    WHERE username in ('jesusfreak', " . $_SESSION['user']['username'] . ")
    AND (requests in ('jesusfreak', " . $_SESSION['user']['username'] . ")
    OR common_friends in ('jesusfreak', " . $_SESSION['user']['username'] . ")
    OR suggest_favorited in ('jesusfreak', " . $_SESSION['user']['username'] . ")
    OR suggest_prayed ('jesusfreak', " . $_SESSION['user']['username'] . ")
    OR suggest_viewed in ('jesusfreak', " . $_SESSION['user']['username'] . "))";

    $conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$conn->query($sql);
$conn->close();

1 个答案:

答案 0 :(得分:0)

试试这个:

 delete from table1 
 where username in ('Jonathan','Amy')
         and (field2 in ('Jonathan','Amy') 
                or 
               field3 in ('Jonathan','Amy')
                or 
              field4 in ('Jonathan','Amy'));