PostgreSQL MySQL 兼容性之 - 空(NULL)

本文涉及的产品
RDS PostgreSQL Serverless,0.5-4RCU 50GB 3个月
推荐场景:
对影评进行热评分析
云数据库 RDS SQL Server,基础系列 2核4GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介:

NULL compare operator <=>

MySQL

SELECT 99 <=> NULL, NULL <=> NULL;
+-------------+---------------+
| 99 <=> NULL | NULL <=> NULL |
+-------------+---------------+
|           0 |             1 |
+-------------+---------------+

IS NULL
IS NOT NULL

PostgreSQL

针对不同的类型,需要创建不同的函数 和 <=>
create or replace function nulleq(int,int) returns int as 
$$

declare
begin
  if $1 is null and $2 is null then
    return 1;
  else
    return 0; 
  end if;
end;

$$
 language plpgsql;

postgres=# create operator <=> (procedure=nulleq,leftarg=int,rightarg=int);
CREATE OPERATOR
postgres=# select 1 <=> null;
 ?column? 
----------
        0
(1 row)

postgres=# select null <=> null;
 ?column? 
----------
        1
(1 row)

IS NULL
IS NOT NULL

coalesce

MySQL

  coalesce

PostgreSQL

postgres=# select coalesce(null,1,2);
 coalesce 
----------
        1
(1 row)

postgres=# select coalesce(null,null,2);
 coalesce 
----------
        2
(1 row)

postgres=# select coalesce('a',null,'b');
 coalesce 
----------
 a
(1 row)

order

MySQL

  SELECT col1 FROM tab ORDER BY ISNULL(col1), col1;  -- null is first
  SELECT col1 FROM tab ORDER BY IF(col1 IS NULL, 0, 1), col1 DESC;  -- Descending order, with NULLs first

  All NULL values are also regarded as equivalent for the purposes of the DISTINCT and GROUP BY clauses.

PostgreSQL

默认nulls比其他值更大
postgres=# create table test(id int);
CREATE TABLE
postgres=# insert into test values (1),(2),(3),(null),(null);
INSERT 0 5
postgres=# select * from test order by test;
 id 
----
  1
  2
  3
   
   
(5 rows)
postgres=# select * from test order by id nulls first;
 id 
----
   
   
  1
  2
  3
(5 rows)
postgres=# select * from test order by id nulls last;
 id 
----
  1
  2
  3
   
   
(5 rows)
postgres=# select * from test order by id desc;
 id 
----
   
   
  3
  2
  1
(5 rows)

postgres=# select * from test order by id desc nulls first;
 id 
----
   
   
  3
  2
  1
(5 rows)

postgres=# select * from test order by id desc nulls last;
 id 
----
  3
  2
  1
   
   
(5 rows)

ISNULL, NULLIF, IFNULL

MySQL

  IFNULL(expr1,expr2)
    If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.

SELECT IFNULL(1,0); 
+-------------+
| IFNULL(1,0) |
+-------------+
|           1 |
+-------------+

SELECT IFNULL(NULL,10);
+-----------------+
| IFNULL(NULL,10) |
+-----------------+
|              10 |
+-----------------+

  NULLIF(expr1,expr2)
    Returns NULL if expr1 = expr2 is true, otherwise returns expr1. This is the same as CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.

SELECT NULLIF(1,1);
+-------------+
| NULLIF(1,1) |
+-------------+
|        NULL |
+-------------+

SELECT NULLIF(1,2);
+-------------+
| NULLIF(1,2) |
+-------------+
|           1 |
+-------------+

  ISNULL(expr)
    If expr is NULL, ISNULL() returns 1, otherwise it returns 0.

SELECT ISNULL(1+1);
+-------------+
| ISNULL(1+1) |
+-------------+
|           0 |
+-------------+

SELECT ISNULL(1/0);
+-------------+
| ISNULL(1/0) |
+-------------+
|           1 |
+-------------+

  ISNULL(expr)
    If expr is NULL, ISNULL() returns 1, otherwise it returns 0.
SELECT ISNULL(1+1);
+-------------+
| ISNULL(1+1) |
+-------------+
|           0 |
+-------------+

SELECT ISNULL(1/0);
+-------------+
| ISNULL(1/0) |
+-------------+
|           1 |
+-------------+

PostgreSQL

postgres=# create or replace function ifnull(int,int) returns int as 
$$

  select case when $1 is not null then $1 else $2 end;

$$
 language sql;
CREATE FUNCTION
postgres=# select ifnull(null,2);
 ifnull 
--------
      2
(1 row)

postgres=# select ifnull(1,3);
 ifnull 
--------
      1
(1 row)

nullif
postgres=# select nullif(1,1);
 nullif 
--------
       
(1 row)

postgres=# select nullif(1,2);
 nullif 
--------
      1
(1 row)

isnull
postgres=# create or replace function isnull(anyelement) returns int as 
$$

select case when $1 is null then 1 else 0 end;              

$$
 language sql;
CREATE FUNCTION

ostgres=# create table ttt(id int);
CREATE TABLE
postgres=# insert into ttt values (null);
INSERT 0 1
postgres=# insert into ttt values (1);
INSERT 0 1
postgres=# select isnull(id),id from ttt;
 isnull | id 
--------+----
      1 |   
      0 |  1
(2 rows)
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1天前
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
|
26天前
|
关系型数据库 MySQL Linux
在Linux中,如何配置数据库服务器(如MySQL或PostgreSQL)?
在Linux中,如何配置数据库服务器(如MySQL或PostgreSQL)?
|
28天前
|
关系型数据库 PostgreSQL
PostgreSQL的null值函数
【8月更文挑战第20天】PostgreSQL的null值函数
36 3
|
27天前
|
关系型数据库 MySQL 分布式数据库
PolarDB 并行查询问题之保证与MySQL的兼容性如何解决
PolarDB 并行查询问题之保证与MySQL的兼容性如何解决
15 1
|
1月前
|
SQL 关系型数据库 MySQL
在 MySQL 中使用 IS NULL
【8月更文挑战第12天】
578 0
在 MySQL 中使用 IS NULL
|
18天前
|
SQL 关系型数据库 MySQL
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
84 0
|
28天前
|
关系型数据库 MySQL 数据库
postgresql使用mysql_fdw连接mysql
通过以上步骤,你可以在PostgreSQL中访问和查询远程MySQL服务器的数据,这对于数据集成和多数据库管理非常有用。
49 0
|
28天前
|
SQL 关系型数据库 MySQL
mysql不等于<>取特定值反向条件的时候字段有null值或空值读取不到数据
对于数据库开发的专业人士来说,理解NULL的特性并知道如何正确地在查询中处理它们是非常重要的。以上所介绍的技巧和实例可以帮助你更精准地执行数据库查询,并确保数据的完整性和准确性。在编写代码和设计数据库结构时,牢记这些细节将有助于你避免许多常见的错误,提高数据库应用的质量与性能。
33 0
|
1月前
|
关系型数据库 MySQL 数据库
探究数据库开源协议:PostgreSQL vs MySQL
探究数据库开源协议:PostgreSQL vs MySQL
|
4月前
|
SQL 关系型数据库 MySQL
实时计算 Flink版产品使用合集之从MySQL同步数据到Doris时,历史数据时间字段显示为null,而增量数据部分的时间类型字段正常显示的原因是什么
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。

相关产品

  • 云数据库 RDS MySQL 版
  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版