从字段与条件不匹配的表中选择

发布于 2024-09-10 09:47:43 字数 209 浏览 3 评论 0原文

我只是想知道我可以执行什么样的 SQL 命令来从某个表中选择所有项目,其中 A 列不等于 x 且 B 列不等于 x代码>

类似:

select something from table
where columna does not equal x and columnb does not equal x

有什么想法吗?

I'm just wondering what kind of SQL command I could execute that would select all items from a certain table where column A is not equal to x and column B is not equal to x

Something like:

select something from table
where columna does not equal x and columnb does not equal x

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

腻橙味 2024-09-17 09:47:43

关键是 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 的值code> 位于当前行,$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)";

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'";

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
}

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.

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)";
飘然心甜 2024-09-17 09:47:43

您可以使用像

NOT columnA = 'x'

Or

columnA != 'x'

Or

columnA <> 'x'

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

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

简化它

Not columnA <=> 'x'

您可以通过<=> 来 是 Null Safe 等于运算符,其中甚至包括空值的结果。

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

(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.

笑,眼淚并存 2024-09-17 09:47:43

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

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

Or can also insert the statement inside bracket.

SELECT * FROM tablename WHERE NOT (columnA = 'x')
随风而去 2024-09-17 09:47:43
select * from table where fiels1 NOT LIKE 'x' AND field2 NOT LIKE 'y'

//此工作以不区分大小写的方式进行

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

//this work in case insensitive manner

如痴如狂 2024-09-17 09:47:43
$sqlquery = "SELECT field1, field2 FROM table WHERE columnA <> 'x' AND columbB <> 'y'";

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

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

八巷 2024-09-17 09:47:43

如果存在空值,上述答案将不起作用。

下面是使用合并来评估可以为 null 的字段的简写:

select * from table where coalesce(my_nullable_field, 0) <> 1

这里我们告诉查询将 NULL 视为 0,而不为 null 添加完全其他的条件,这将使接线简单并且代码易于阅读。

Above answers will not work if there's a null value.

Here's a shorthand to evaluate fields that can be null using coalesce:

select * from table where coalesce(my_nullable_field, 0) <> 1

Here we tell the query to treat NULL as a 0, without adding a completely other condition for nulls, which would keep wiring simple and the code easily readable.

千紇 2024-09-17 09:47:43

您还可以使用

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

           

You can use also

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文