mysql: '哪里有东西!=true'排除带有 NULL 的字段

发布于 2024-10-22 07:30:15 字数 213 浏览 1 评论 0 原文

我有两个表,一个表中有组,另一个表设置了可以看到哪些组的用户限制。 当我执行 LEFT JOIN 且未指定任何条件时,它会显示所有记录。当我执行 WHERE group_hide.hide!='true' 时,它仅显示这些设置为 false 枚举类型的记录。通过 JOIN,其他组将隐藏字段设置为“NULL”。 我怎样才能使它只排除那些设置为 true 的内容,并显示其他所有 NULL 或 false 的内容?

I have a 2 tables, one in which I have groups, the other where I set user restrictions of which groups are seen.
When I do LEFT JOIN and specify no condition, it shows me all records. When I do WHERE group_hide.hide!='true' it only shows these records that have false enum type set to them. With JOIN, other groups get the hide field set as "NULL".
How can I make it so that it excludes only these that are set to true, and show everything else that has either NULL or false?

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

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

发布评论

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

评论(3

星星的轨迹 2024-10-29 07:30:15

在 MySQL 中,处理可为空值时必须使用 IS NULLIS NOT NULL

这里你应该使用 (group_hide.hide IS NULL OR group_hide.hide != 'true')

In MySQL you must use IS NULL or IS NOT NULL when dealing with nullable values.

HEre you should use (group_hide.hide IS NULL OR group_hide.hide != 'true')

倦话 2024-10-29 07:30:15

唐已经对您提出的问题提供了很好的答案,并将解决您眼前的问题。

但是,让我解决数据类型域错误的问题。通常你会将 hide 设置为 BOOLEAN,但 mysql 并没有真正完全实现它。它将其转换为 TINYINT(1),允许值从 -128 到 127(请参阅 mysql 的数据类型)。由于 mysql 不支持 CHECK 约束,因此您可以选择使用触发器或外部引用来正确强制执行域。

以下是错误数据域(您的情况)的问题,按重要性顺序排列:

  • 对于只能为 1 或 0 的字段允许 NULL 的缺点是您必须使用 3 值逻辑(true、false、null),顺便说一句,它在 SQL 中并未完美实现。这使得某些查询比它们需要的更复杂更慢。如果您可以将列设置为 NOT NULL,请执行此操作。
  • 对只能为 1 或 0 的字段使用 VARCHAR 的缺点是查询速度,因为需要额外的 I/O 和更大的存储需求(如果字段是字段的一部分,则会减慢读取、写入速度,使索引更大)索引并影响备份的大小;请记住,对于较小的单个字段,这些影响可能不会明显。大小表,但如果数据类型始终设置得太大或者表的记录数量过多,则会产生影响)。此外,您始终需要将 VARCHAR 转换为 1 或 0 以使用自然的 mysql 布尔运算符,这会增加查询的复杂性。
  • mysql 使用 TINYINT(1) 表示 BOOL 的缺点是 RDBMS 允许某些不应该允许的值,理论上允许在系统中存储无意义的值。在这种情况下,您的应用程序层必须保证数据完整性,如果 RDBMS 保证完整性总是更好,因为它可以保护您免受应用程序层中的某些错误以及数据库管理员可能犯的错误的影响。

Don already provided good answer to the question that you asked and will solve your immediate problem.

However, let me address the point of wrong data type domain. Normally you would make hide be BOOLEAN but mysql does not really implement it completely. It converts it to TINYINT(1) which allows values from -128 to 127 (see overview of data types for mysql). Since mysql does not support CHECK constraint you are left with options to either use a trigger or foreign reference to properly enforce the domain.

Here are the problems with wrong data domain (your case), in order of importance:

  • The disadvantages of allowing NULL for a field that can be only 1 or 0 are that you have to employ 3 value logic (true, false, null), which btw is not perfectly implemented in SQL. This makes certain query more complex and slower then they need to be. If you can make a column NOT NULL, do.
  • The disadvantages of using VARCHAR for a field that can be only 1 or 0 are the speed of the query, due to the extra I/O and bigger storage needs (slows down reads, writes, makes indexes bigger if a field is part of the index and influences the size of backups; keep in mind that none of these effects might be noticeable with wrong domain of a single field for a smaller size tables, but if data types are consistently set too big or if the table has serious number of records the effects will bite). Also, you will always need to convert the VARCHAR to a 1 or 0 to use natural mysql boolean operators increasing complexity of queries.
  • The disadvantage of mysql using TINYINT(1) for BOOL is that certain values are allowed by RDBMS that should not be allowed, theoretically allowing for meaningless values to be stored in the system. In this case your application layer must guarantee the data integrity and it is always better if RDBMS guarantees integrity as it would protect you from certain bugs in application layer and also mistakes that might be done by database administrator.
二手情话 2024-10-29 07:30:15

一个明显的答案是:

WHERE (group_hide.hide is null or group_hide.hide ='false')

我完全不确定空行为规则是什么。

an obvious answer would be:

WHERE (group_hide.hide is null or group_hide.hide ='false')

I'm not sure off the top of my head what the null behaviour rules are.

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