[根据特定条件从表中选择'描述'

问题描述 投票:0回答:2

我有两个表:

table1

**column0**       **description**
foo               this
bar               that
hello             yellow
bye               sky
...
baz               fred

table2

**column1**       **column2**
foo
bar               hello
...
baz               bye

Column0在column1和column2中具有所有内容

我要根据以下条件从表1中选择描述:

1]如果column2为空,则选择描述,其中column1 = column0。

2)否则,显示描述,其中column2 = column0

请让我知道是否需要澄清。我正在尝试在Postgresql中做到这一点。

****************更新****************:

基于您从表中看到的信息的结果将是

  1. 对'foo'的描述,它是'this',因为column2在'foo'旁边是空的]]] >>

  2. 'hello'的说明,因为在第2列中该字段不为空

  3. 'bye'的描述,因为在第2列中该字段不为空

  4. 因此:

    this
    
    yellow
    
    sky
    

我有两个表:table1 ** column0 ** ** description ** foo这个酒吧,你好黄色再见的天空... baz fred table2 ** column1 * ...

sql database postgresql
2个回答
0
投票

您需要加入表并在COALESCE()子句中使用ON,如下所示:

select t1.description
from table2 t2 inner join table1 t1
on coalesce(t2.column2, t2.column1) = t1.column0

0
投票

尝试一下:

© www.soinside.com 2019 - 2024. All rights reserved.