简体   繁体   English

在PostgreSQL的查询中替换SELECT返回值

[英]Substituting SELECT return values in a postgres' query

I've got an issue to solve in a postgres model query. 我在postgres模型查询中要解决一个问题。

The down below query returns me a table with three columns: "central", "imsi" and "mapver". 下面的查询向我返回一个包含三列的表:“中心”,“ imsi”和“映射器”。 Sometimes, the query will return blank values in "mapver", but i can't let a blank space exist into it. 有时,查询会在“ mapver”中返回空白值,但是我不能让空白存在。 | | How can i make this query substitute the blanks by "-" or a word, like "WrongBlank"? 如何使此查询用“-”或单词(例如“ WrongBlank”)代替空格? It is a varchar field. 这是一个varchar字段。

SELECT Test_Configs.central, Test_Configs.imsi, 
       Test_Configs.mapver      

FROM config_imsis_centrais AS Default_Configs              -- Valores padrão da central correta
    LEFT JOIN config_imsis_centrais AS Test_Configs        -- Valores das centrais a serem testadas
        ON Default_Configs.central = 'ZBLM04'
        AND Default_Configs.ts = (SELECT MAX(ts) FROM config_imsis_centrais)
        AND Default_Configs.imsi = Test_Configs.imsi
        AND Default_Configs.ts = Test_Configs.ts
        AND Test_Configs.central <> Default_Configs.central
WHERE (                                                    -- Análise:
            COALESCE(Default_Configs.mapver, 'null') <> COALESCE(Test_Configs.mapver, 'null') AND
            Test_Configs.central <> ''
       )

A more simple example... I often get: 一个更简单的示例...我经常得到:

central  |   imsi   |   mapver
--------------------------------
ZSPO03   |   74402  |   
ZSPO03   |   74401  |   
ZSPO03   |   72434  |   
ZSPO03   |   72415  |

But I want: 但我想要:

central  |   imsi   |   mapver
--------------------------------
ZSPO03   |   74402  |     -
ZSPO03   |   74401  |     -
ZSPO03   |   72434  |     -
ZSPO03   |   72415  |     -

Thanks a lot! 非常感谢!

http://www.postgresql.org/docs/current/static/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL http://www.postgresql.org/docs/current/static/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL

The COALESCE function returns the first of its arguments that is not null. COALESCE函数返回其第一个不为null的参数。 Null is returned only if all arguments are null. 仅当所有参数均为null时才返回Null。 It is often used to substitute a default value for null values when data is retrieved for display, for example: 在检索数据进行显示时,通常用它来用默认值代替空值,例如:

SELECT COALESCE(description, short_description, '(none)')

A text constant can never be null. 文本常量永远不能为null。 So, if the things before it are null, the text string will be returned. 因此,如果之前的内容为null,则将返回文本字符串。

SELECT Test_Configs.central, Test_Configs.imsi, 
       COALESCE(Test_Configs.mapver, 'whatever string you want')      

FROM config_imsis_centrais AS Default_Configs              -- Valores padrão da central correta
    LEFT JOIN config_imsis_centrais AS Test_Configs        -- Valores das centrais a serem testadas
        ON Default_Configs.central = 'ZBLM04'
        AND Default_Configs.ts = (SELECT MAX(ts) FROM config_imsis_centrais)
        AND Default_Configs.imsi = Test_Configs.imsi
        AND Default_Configs.ts = Test_Configs.ts
        AND Test_Configs.central <> Default_Configs.central
WHERE (                                                    -- Análise:
            COALESCE(Default_Configs.mapver, 'null') <> COALESCE(Test_Configs.mapver, 'null') AND
            Test_Configs.central <> ''
       )

EDIT, has empty strings, not nulls 编辑,具有空字符串,不为空

Reference: http://www.postgresql.org/docs/9.3/static/functions-conditional.html#FUNCTIONS-CASE 参考: http : //www.postgresql.org/docs/9.3/static/functions-conditional.html#FUNCTIONS-CASE

SELECT Test_Configs.central, Test_Configs.imsi, 
       CASE Test_Configs.mapver WHEN '' THEN '-'
           ELSE COALESCE(Test_Configs.mapver, '-') 
       END AS mapver  

FROM config_imsis_centrais AS Default_Configs              -- Valores padrão da central correta
    LEFT JOIN config_imsis_centrais AS Test_Configs        -- Valores das centrais a serem testadas
        ON Default_Configs.central = 'ZBLM04'
        AND Default_Configs.ts = (SELECT MAX(ts) FROM config_imsis_centrais)
        AND Default_Configs.imsi = Test_Configs.imsi
        AND Default_Configs.ts = Test_Configs.ts
        AND Test_Configs.central <> Default_Configs.central
WHERE (                                                    -- Análise:
            COALESCE(Default_Configs.mapver, 'null') <> COALESCE(Test_Configs.mapver, 'null') AND
            Test_Configs.central <> ''
       )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM