1

I have the following scenario.I have 3 tables with the following structure.

TABLE A
 -entry_id (PRIMARY KEY, INTEGER)

TABLE B 
 -entry_id (FOREIGN_KEY -> TABLE A)
 -content (TEXT)

TABLE C
  -entry_id (FOREIGN_KEY -> TABLE A)
  -content (INTEGER)

I want to retrive the content cell value from either table B or table C. The value can be in just one of the table. So it is either table B or C witch have an entry with a given entry_id.

PS. Sorry if duplicate did not manage to find anything to match what i need.

1
  • Would like something that works with also a 4th table. For example: TABLE D -entry_id (FOREIGN_KEY -> TABLE A) -content (DOUBLE) Commented Jun 20, 2017 at 9:37

2 Answers 2

1

If I correctly understand, you need something like:

select entry_id, content::text from TABLEB where entry_id = ?
union all
select entry_id, content::text from TABLEC where entry_id = ?
union all
select entry_id, content::text from TABLED where entry_id = ?
Sign up to request clarification or add additional context in comments.

2 Comments

No my problem is that i have an entry_id and the value for that entry_id can be in any of the 3 tables(a value can only be in one table for and entry_id).So i want based on the entry_id to retrive the corespondent data from one of the other 3 tables.Does it make sense?
@Cosmin_Victor - then add where clause to all query, please check edited answer.
1

If it can only exist in one table at a time, use a union

select a1.entry_id, b2.content
from TableA a1
inner join TableB b2
on a1.entry_id = b2.entry_id

union -- This removes any duplicates. Use UNION ALL to show duplicates

select a1.entry_id, c3.content::text
from TableA a1
inner join TableC c3
on a1.entry_id = c3.entry_id

2 Comments

UNION will remove duplicates, why you predicate "only exist in one table at a time"?..
1) UNION types integer and text cannot be matched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.