可能具有Join和NULL值的特定SQL查询

时间:2015-09-23 12:59:12

标签: sql postgresql join

这是问题,

我想要使用2个表,

表1:

t1_id | t1_name | t1_reference | t1_related | t1_comment

t1_id is a serial
t1_name is a text field
t1_reference is an integer
t1_related is a foreign key pointing to t2_id, is NOT REQUIRED (default NULL)
t1_comment is a text field

表2:

t2_id | t2_name

t2_id is a serial
t2_name is a text field

我想要做的是在存在时检索所有t1字段和t2_name字段,其中t1_reference = someValue

我对查询中的连接感到困惑,我不明白它如何与字段中可能存在的非现有值...对我来说,查询应如下所示:

SELECT t1.*,t2_name FROM t1 [... ??? join part ??? ...] WHERE t1_reference = value

实际上正在研究PostgreSQL。

感谢您阅读/帮助

1 个答案:

答案 0 :(得分:1)

您可以通过以下逻辑获得所需的结果(语法可能不同):

SELECT t1.*,t2.t2_name FROM t1, t2 WHERE t1.t1_related=t2.t2_id and t1_reference = ~value~