Jef*_*ake 69
关键是sql查询,您将设置为字符串:
$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";
Run Code Online (Sandbox Code Playgroud)
请注意,有很多方法可以指定NOT.另一个同样有效的是:
$sqlquery = "SELECT field1, field2 FROM table WHERE columnA != 'x' AND columbB != 'y'";
Run Code Online (Sandbox Code Playgroud)
以下是如何使用它的完整示例:
$link = mysql_connect($dbHost,$dbUser,$dbPass) or die("Unable to connect to database");
mysql_select_db("$dbName") or die("Unable to select database $dbName");
$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";
$result=mysql_query($sqlquery);
while ($row = mysql_fetch_assoc($result) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
你可以在上面的while循环中做任何你想做的事情.访问表的每个字段作为其中的元素,$row array
这意味着$row['field1']
将为您提供field1
当前行$row['field2']
的值,并为您提供值field2
.
请注意,如果列可能具有NULL
值,则使用上述任一语法都无法找到这些值.您需要添加子句以包含NULL
值:
$sqlquery = "SELECT field1, field2 FROM table WHERE (NOT columnA = 'x' OR columnA IS NULL) AND (NOT columbB = 'y' OR columnB IS NULL)";
Run Code Online (Sandbox Code Playgroud)
你可以使用喜欢
NOT columnA = 'x'
Run Code Online (Sandbox Code Playgroud)
要么
columnA != 'x'
Run Code Online (Sandbox Code Playgroud)
要么
columnA <> 'x'
Run Code Online (Sandbox Code Playgroud)
和Jeffly Bake的查询一样,对于包含空值,你不必写得像
(NOT columnA = 'x' OR columnA IS NULL)
Run Code Online (Sandbox Code Playgroud)
你可以简单地做到
Not columnA <=> 'x'
Run Code Online (Sandbox Code Playgroud)
<=>是Null Safe等于Operator,其中包含偶数空值的结果.