从不等于 Mysql Php 的字段中选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3286644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:14:21  来源:igfitidea点击:

Select from where field not equal to Mysql Php

phpmysqlsqldatabaseselect

提问by Belgin Fish

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

我只是想知道我可以在 php 中执行什么样的 mysql 命令,它会从某个表中选择所有项目,其中 columna 不等于 x 且 columnb 不等于 x

Something like: 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

Any ideas?

有任何想法吗?

回答by Jeffrey Blake

The key is the sql query, which you will set up as a string:

关键是 sql 查询,您将其设置为字符串:

$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";

Note that there are a lot of ways to specify NOT. Another one that works just as well is:

请注意,有很多方法可以指定 NOT。另一个同样有效的方法是:

$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
}

You can do whatever you would like within the above while loop. Access each field of the table as an element of the $row arraywhich means that $row['field1']will give you the value for field1on the current row, and $row['field2']will give you the value for field2.

你可以在上面的 while 循环中做任何你想做的事情。访问表的每个字段作为 的元素,$row array这意味着这$row['field1']将为您提供field1当前行$row['field2']的值,并将为您提供值field2

Note that if the column(s) could have NULLvalues, those will not be found using either of the above syntaxes. You will need to add clauses to include NULLvalues:

请注意,如果列可以有NULL值,则使用上述任何一种语法都无法找到这些值。您需要添加子句以包含NULL值:

$sqlquery = "SELECT field1, field2 FROM table WHERE (NOT columnA = 'x' OR columnA IS NULL) AND (NOT columbB = 'y' OR columnB IS NULL)";

回答by Sayka

You can use like

你可以使用像

NOT columnA = 'x'

Or

或者

columnA != 'x'

Or

或者

columnA <> 'x'

And like Jeffly Bake's query, for including null values, you don't have to write like

就像 Jeffly Bake 的查询一样,对于包含空值,您不必像这样写

(NOT columnA = 'x' OR columnA IS NULL)

You can make it simple by

你可以通过

Not columnA <=> 'x'

<=>is the Null Safe equal to Operator, which includes results from even null values.

<=>是 Null Safe 等于 Operator,它包括偶数空值的结果。

回答by hazari zulkefli

Or can also insert the statement inside bracket.

或者也可以在括号内插入语句。

SELECT * FROM tablename WHERE NOT (columnA = 'x')

回答by Affan

select * from table where fiels1 NOT LIKE 'x' AND field2 NOT LIKE 'y'

//this work in case insensitive manner

//这项工作以不区分大小写的方式工作

回答by Prashant Tapase

$sqlquery = "SELECT field1, field2 FROM table WHERE columnA <> 'x' AND columbB <> 'y'";

I'd suggest using the diamond operator (<>) in favor of != as the first one is valid SQL and the second one is a MySQL addition.

我建议使用菱形运算符 (<>) 来代替 !=,因为第一个是有效的 SQL,第二个是 MySQL 的补充。

回答by KhabaNganba

You can use also

你也可以使用

select * from tablename where column1 ='a' and column2!='b';

?????? ?????

???????????