gpt4 book ai didi

mysql - 列同时不能等于某个值(mysql)

转载 作者:行者123 更新时间:2023-11-29 19:18:57 25 4
gpt4 key购买 nike

我有这张表:

+------+----------+-------------+
| id | category | category_id |
+------+----------+-------------+
| 1 | 2 | 12 |
| 2 | 1 | 12 |
| 3 | 3 | 54 |
| 4 | 3 | 17 |
| 5 | 2 | 14 |
+------+----------+-------------+

我需要获得以下内容:
当类别 = 2 时:category_id != 12

+------+----------+-------------+
| id | category | category_id |
+------+----------+-------------+
| 2 | 1 | 12 |
| 3 | 3 | 54 |
| 4 | 3 | 17 |
| 5 | 2 | 14 |
+------+----------+-------------+

此代码不起作用:

SELECT * FROM `table` WHERE `category`!=2 AND `category_id`!=12

最佳答案

我想你想要OR:

select *
from `table`
where `category` != 2
or `category_id` != 12

根据De Morgan's law ,等同于:

select *
from `table`
where not (
`category` = 2
and `category_id` = 12
)

内容为 - 获取除类别为 2 且category_id 为 12 的所有行(在一行中)

关于mysql - 列同时不能等于某个值(mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com