Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6241281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:44:23+00:00 2026-05-24T11:44:23+00:00

There are 3 table in postgres database CREATE TABLE tab_name ( name_id integer NOT

  • 0

There are 3 table in postgres database

CREATE TABLE tab_name
(
  name_id integer NOT NULL,
  cust_name character varying NOT NULL, -- contains names like david,jones,athur
  CONSTRAINT tab_name_pkey PRIMARY KEY (name_id)
)

CREATE TABLE tab_rel
(
  rel_id integer NOT NULL,
  rel_desc character varying NOT NULL,-- contains relation description father son, sister brother
  CONSTRAINT tab_rel_pkey PRIMARY KEY (rel_id)
)

CREATE TABLE tab_rel_map
(
  rel_id integer NOT NULL,
  name_id1 integer NOT NULL,
  name_id2 integer NOT NULL,
  CONSTRAINT tab_rel_map_name_id1_fkey FOREIGN KEY (name_id1)
      REFERENCES tab_name (name_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT tab_rel_map_name_id2_fkey FOREIGN KEY (name_id2)
      REFERENCES tab_name (name_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT tab_rel_map_rel_id_fkey FOREIGN KEY (rel_id)
      REFERENCES tab_rel (rel_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

I am trying to write function which take rel_id as input and should output cust_name respect to name_id1 and name_id2. Because both name_id1 and name_id2 references to same parent id I am not able to get respective name.

rel_id | relation     | cust_name1 | cust_name2
------------------------------------------------
1      | Father son   | David      | Jones
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:44:24+00:00Added an answer on May 24, 2026 at 11:44 am

    You need to join to the same table twice – here’s how you do that using aliases:

    select 
        rm.rel_id,
        r.rel_desc as relation,
        n1.cust_name as cust_name1,
        n2.cust_name as cust_name2
    from tab_rel_map rm
    join tab_rel r on r.id = rm.rel_id
    left join tab_name n1 on n1.name_id = rm.name_id1
    left join tab_name n2 on n2.name_id = rm.name_id2
    where rm.rel_id = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a table: create table table1 ( id integer primary key, user_id varchar(36),
I have a postgres database with a table that contains rows I want to
In a MySQL (5.1) database table there is data that represents: how long a
There is a table: doc_id(integer)-value(integer) Approximate 100.000 doc_id and 27.000.000 rows. Majority query on
Let's say there's a table created as follows: create table testTable ( colA int
Consider i have a registartion table and there is field prefLocationId and it contains
I have an Postgre database and a table called my_table. There are 4 columns
I want to create a postgres user that can access only one database on
I'm designing an app with a rather complex table design using the Postgres database,
Is there a way to create a backup of a single table within a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.