Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

postgresql - SQL - fixing Join issue

I have a query like below.

select   c.relname as Table,ex.location as path,
case when ex.fmttype='t' then 'text' when ex.fmttype='c' then 'csv'
else 'Unknown' end as format, case when ex.writable='0' then 'Yes' else 'No' end as is_readonly
from pg_exttable ex 
join pg_class c on ex.reloid=c.oid;

On the table pg_exttable I have a column called reloid. I want to add a new column in this query from another table. On the other table the column oid can be a join condition.

So I tried this query.

select  ns.nspname as schema,c.relname as Table,ex.location as path,
case when ex.fmttype='t' then 'text' when ex.fmttype='c' then 'csv'
else 'Unknown' end as format, case when ex.writable='0' then 'Yes' else 'No' end as is_readonly
from pg_exttable ex 
join pg_class c on ex.reloid=c.oid 
join pg_catalog.pg_namespace ns on ex.reloid=ns.oid  ;

But its giving 0 rows. I dont know where is the issue with this join.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

Seems there are no rows to join to the existing result in the last table. You can use left outer join to fill the missing values with null:

select  
    ns.nspname as schema,
    c.relname as Table,
    ex.location as path,
    case 
        when ex.fmttype = 't' then 'text' 
        when ex.fmttype = 'c' then 'csv'
        else 'Unknown' 
    end as format, 
    case 
        when ex.writable = '0' then 'Yes' 
        else 'No' 
    end as is_readonly
from pg_exttable ex 
join pg_class c on ex.reloid = c.oid 
left outer join pg_catalog.pg_namespace ns on ex.reloid = ns.oid;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...