Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 根据特定条件从表中选择“说明”_Sql_Database_Postgresql - Fatal编程技术网

Sql 根据特定条件从表中选择“说明”

Sql 根据特定条件从表中选择“说明”,sql,database,postgresql,Sql,Database,Postgresql,我有两张桌子: 表1 **column0** **description** foo this bar that hello yellow bye sky ... baz fred 表2 **column1** **column2** foo bar hello ... baz b

我有两张桌子:

表1

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

**column1**       **column2**
foo
bar               hello
...
baz               bye
Column0在column1和column2中包含所有内容

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

1如果column2为空,则选择column1=column0的描述

2否则,显示column2=column0的描述

如果需要任何澄清,请告诉我。我正试图在postgresql中实现这一点

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

根据您可以从表中看到的信息得出的结果将是

作为第2列的“this”为“foo”的“foo”的描述在“foo”旁边为空

“hello”的描述,因为在第2列中不是空的

“bye”的说明,因为第2列中不为空

因此:

this

yellow

sky

您需要连接这些表并在ON子句中使用coalescin,如下所示:

select t1.description
from table2 t2 inner join table1 t1
on coalesce(t2.column2, t2.column1) = t1.column0
您可能还需要一个ORDERBY子句,以便按照您想要的顺序获得结果。 看。 结果:

试试这个:

select test from(
select case when t2.column2 is null then t1.description
            else null
      end test
from table1 t1
left join table2 t2
on t1.column0 = t2.column1) A
where test is not null;
以下是我认为存在的可能就足够了:

select t1.description
from table1 t1
where exists (select 1
              from table2 t2
              where t2.column1 = t1.column0
             ) or
      exists (select 1
              from table2 t2
              where t2.column2 = t1.column0
             );
也就是说,您的具体规则似乎是:

select t1.description
from table1 t1 left join
     table2 t2
     on t2.column1 = t1.column0 and
        t2.column2 is null left join
     table2 tt2
     on tt2.column2 = t1.column0 and
        t2.column1 is null
where t2.column1 is not null or
      tt2.column2 is not null;

1你想要什么样的结果?2您使用的是什么数据库?我删除了不兼容的数据库标记。3你说Column0包含column1和column2中的所有内容,但是样本数据没有提供任何证据。Gordon Linoff-样本数据没有提供任何证据,这正是我说它有的原因。我现在只为你编辑了这篇文章。你尝试了什么?威尔-就我的SQL技能而言,我只能得出这篇文章。它只能得到column1的描述选择table1。table1中的描述内部连接table2在table1上。column0=table2。column1你详细阐述的内容是否有效?你有错误吗?错误的结果?你检查演示了吗?它适用于您提供的示例数据和逻辑。没有错误-只是错误的结果。你是对的,虽然我不知道如何使用它,但我确实需要一份订单。为什么结果不正确?您可以在演示中看到结果是正确的。表中没有顺序。您可以使用ORDERBY子句定义order。您是否有一列对结果进行排序?如果你真的使用它。如果没有,那么就没有顺序。我试过了,但是第二列缺少了一些描述。你这么说是什么意思?第二列缺少了一些描述?
select t1.description
from table1 t1 left join
     table2 t2
     on t2.column1 = t1.column0 and
        t2.column2 is null left join
     table2 tt2
     on tt2.column2 = t1.column0 and
        t2.column1 is null
where t2.column1 is not null or
      tt2.column2 is not null;