1

我正在使用 Postgres 并具有以下 SQL 语句:

SELECT *
FROM "osmlocal-dsd-de".t_osm_vehicle_image t 
WHERE t.vehicle_config_id = 3 
  and image_type_id = 2

返回一行:

id  vehicle_config_id       cosy_url       image_type_id
113         3               SomeValue             2

当我运行以下命令时:

SELECT * from "osmlocal-dsd-de".t_osm_vehicle_image t 
WHERE t.vehicle_config_id = 3 
  and image_type_id = 2 
  and coalesce(t.cosy_url, '') = ''

返回零行。

我认为我的理解coalesce是错误的,因为我本以为仍然会返回一行,因为cosy_urlis not null

任何关于我做错了什么的建议将不胜感激。

4

2 回答 2

3

您对合并的理解是错误的

它返回第一个不为空的参数。如果所有参数都为 null,则 COALESCE 函数将返回 null

在你的情况下t.cosy_url不是 null 它是平等SomeValue的,你的条件不起作用,因为SomeValue不等于''

于 2018-07-24T11:05:34.783 回答
3

你好像误会了coalesce()。它返回不是的第一个值null

在您的情况下,您有:

coalesce(t.cosy_url, '') 

因为 t.cosy_url 有一个值 ( 'SomeValue'),所以计算结果为那个值。值不是''这样,表达式返回 false 并且整个where子句返回 false。

如果您想要非NULL值,请使用:

t.cosy_url is not null
于 2018-07-24T11:02:30.410 回答