From 0c7cefa467e425d706923ce8d0b8926677a41e6f Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Sun, 12 Jan 2025 00:07:13 +0700 Subject: [PATCH] sql null --- docs/2022-07-31-sql.html | 24 ++++++++++++++++++++++-- posts/2022-07-31-sql.md | 7 +++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/docs/2022-07-31-sql.html b/docs/2022-07-31-sql.html index f59f83b..cf6452e 100644 --- a/docs/2022-07-31-sql.html +++ b/docs/2022-07-31-sql.html @@ -283,7 +283,11 @@

Be careful with NULL

FROM table WHERE (field_something IS NULL OR field_something != 1) -

Need clarify why this happen? Idk :(

+

+ Because NULL is not equal to anything, even + NULL != NULL, we only can check with IS NULL or + IS NOT NULL. +

VARCHAR or TEXT

Thanks SQL best practices – don’t compare count(*) with 0SQL best practices – don’t compare count with 0 +

+ +
  • +

    + SQL NULLs are Weird!

    +
  • diff --git a/posts/2022-07-31-sql.md b/posts/2022-07-31-sql.md index bfe501f..db97f2a 100644 --- a/posts/2022-07-31-sql.md +++ b/posts/2022-07-31-sql.md @@ -131,7 +131,8 @@ FROM table WHERE (field_something IS NULL OR field_something != 1) ``` -Need clarify why this happen? Idk :( +Because `NULL` is not equal to anything, even `NULL != NULL`, we only can check +with `IS NULL` or `IS NOT NULL`. ## `VARCHAR` or `TEXT` @@ -192,4 +193,6 @@ FROM information_schema.statistics; - [Difference between text and varchar (character varying)](https://stackoverflow.com/a/4849030) - [How to get the number of total results when there is LIMIT in query?](https://stackoverflow.com/q/33889922) - [Run a query with a LIMIT/OFFSET and also get the total number of rows](https://stackoverflow.com/q/28888375) -- [SQL best practices – don’t compare count(*) with 0](https://www.depesz.com/2024/12/01/sql-best-practices-dont-compare-count-with-0/) +- [SQL best practices – don’t compare count with 0](https://www.depesz.com/2024/12/01/sql-best-practices-dont-compare-count-with-0/) +- [SQL NULLs are Weird!](https://jirevwe.github.io/sql-nulls-are-weird.html) + - [NULL Handling in SQLite Versus Other Database Engines](https://www.sqlite.org/nulls.html)