选择从哪里字段不等于Mysql Php [英] Select from where field not equal to Mysql Php
问题描述
我只是想知道什么样的mysql命令我可以在php中执行,将选择从某个表中的所有项目,其中columna不等于x和columnb不等于x
I'm just wondering what kind of mysql command i could execute in php that would select all items from a certain table where columna is not equal to x and columnb is not equal to x
类似:从表中选择一个列,其中columna不等于x,columnb不等于x
任何想法?
推荐答案
关键是sql查询,您将设置为字符串:
The key is the sql query, which you will set up as a string:
$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";
请注意,有很多方法可以指定NOT。另一个工作原理是:
Note that there are a lot of ways to specify NOT. Another one that works just as well is:
$sqlquery = "SELECT field1, field2 FROM table WHERE columnA != 'x' AND columbB != 'y'";
下面是如何使用它的完整示例:
Here is a full example of how to use it:
$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数组的一个元素
这意味着 $ row ['field1']
将为当前行提供 field1
的值, $ row ['field2']
将为您提供 field2
的值。
You can do whatever you would like within the above while loop. Access each field of the table as an element of the $row array
which means that $row['field1']
will give you the value for field1
on the current row, and $row['field2']
will give you the value for field2
.
请注意,如果列可以有 NULL
值,那么将不会使用上述任何一种语法找到。包括 NULL
值:
Note that if the column(s) could have NULL
values, those will not be found using either of the above syntaxes. You will need to add clauses to include NULL
values:
$sqlquery = "SELECT field1, field2 FROM table WHERE (NOT columnA = 'x' OR columnA IS NULL) AND (NOT columbB = 'y' OR columnB IS NULL)";
这篇关于选择从哪里字段不等于Mysql Php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!