我只是想知道我可以在php中执行哪种mysql命令,它将从某个表中选择所有项目,其中columna不等于x并且columnb不等于x
就像是: select something from table where columna does not equal x and columnb does not equal x
select something from table where columna does not equal x and columnb does not equal x
有任何想法吗?
关键是sql查询,您将其设置为字符串:
$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";
请注意,有很多方法可以指定NOT。另一个效果很好的是:
$sqlquery = "SELECT field1, field2 FROM table WHERE columnA != 'x' AND columbB != 'y'";
这是如何使用它的完整示例:
$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 }
您可以在上述while循环内做任何您想做的事情。作为元素访问表中的每个字段,$row array这意味着$row['field1']将为您提供field1当前行$row['field2']的值,并为您提供的值field2。
$row array
$row['field1']
field1
$row['field2']
field2
请注意,如果列中可能有NULL值,则使用上述两种语法都不会找到这些值。您将需要添加子句以包含NULL值:
NULL
$sqlquery = "SELECT field1, field2 FROM table WHERE (NOT columnA = 'x' OR columnA IS NULL) AND (NOT columbB = 'y' OR columnB IS NULL)";