Postgres SQL SELECT data from the table the entry exists with ID - sql

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.

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 = ?

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

Related

SQL query to append values not contained in second table

I have table A and table B with different number of columns but both containing a column with IDs. Table A contains more complete list of IDs and table B contains some of the IDs from the table A.
I would like to return resulting table B with original information plus appended IDs that are missing in B but contained in A. For these appended rows, other columns should be blank while column with IDs in B should just contain missing ID values.
Simple solution UNION ALL, with NOT EXISTS:
select b.id, b.c1, ..., b.cn
from b
UNION ALL
select distinct a.id, null, ..., null -- should be same number of columns as in the above select
from a
where not exists (select 1 from b where b.id = a.id)
I think you described left join:
select *
from b left join
a
using (id)

SQL Request for joining a table and printing its column twice in the result table

There are two tables (A, B) in my database and I'm trying to join them and print the same column of table B twice in the result table. The problem is that I need to put in compliance with two different columns of table A, so to join these tables also twice. I tried to join them and put in the select expression required column two times, but in this case result table shows the same relation in every column.
Here is the result I want, where the id column of table B includes id_1 and id_2 from table A and the output names correspond to ids from table A.
Table A: (integer id_1, integer id_2)
Table B: (integer id, varchar name)
Result Table: (id_1, name, id_2, name)
You need inner join with tableB twice as follows:
select a.id_1, b1.name, a.id_2, b2.name
from tableA a join tableB b1 on a.id_1 = b1.id
join tableB b2 on a.id_2 = b2.id

DB2 How to insert values into a table based on 2 other tables?

I have 3 tables:
Table A : obj_1 (varchar), rlt(varchar), obj_2 (varchar)
Table B : r_id (int), r_obj (varchar)
Table C: obj1 (int), action(varchar), obj2(int)
I need to insert into Table C so it is the exact copy of Table A, except instead of the obj_1 and obj_2 names, it uses the reference number for that object (r_id) from Table C.
INSERT into tablec (obj1, action, obj2) ((select r_id from tableb, tablea
where tablea.obj_1 = tableb.r_obj), (select rlt from tablea), (select r_id
from tableb, tablea where tablea.obj_1 = tableb.r_obj))
You can use an insert-select statement with a query that joins reference and project:
INSERT INTO tablec (obj1, action, obj2)
(SELECT b1.r_id, a.action, b2.r_id
FROM tablea a
JOIN tableb b1 ON a.obj1 = b1.r_obj
JOIN tableb b2 ON a.obj2 = b2.r_obj)
I had to use a workaround since db2 has certain limitations:
I used 2 temporary views as stepping stones to my goal.
I do the 2 joins required separately:
create view temp1 (a, b) as (select tablea.p_id, tableb.r_id from tablea
tableb where tablea.obj_1 = tableb.r_obj)
create view temp2 (c, d, rlt) as (select tablea.p_id, tableb.r_id, tablea.rlt
from tablea, tableb where tablea.obj_2 = tableb.r_obj)
then I used one insert statament to combine these 2 based on the PK from tablea
INSERT INTO final (obj_1, rlt, obj_2)
(select temp1.b, temp2.rlt, temp2.d from temp1 join temp2 on temp1.a = temp2.c)

Value present in more than one table

I have 3 tables. All of them have a column - id. I want to find if there is any value that is common across the tables. Assuming that the tables are named a.b and c, if id value 3 is present is a and b, there is a problem. The query can/should exit at the first such occurrence. There is no need to probe further. What I have now is something like
( select id from a intersect select id from b )
union
( select id from b intersect select id from c )
union
( select id from a intersect select id from c )
Obviously, this is not very efficient. Database is PostgreSQL, version 9.0
id is not unique in the individual tables. It is OK to have duplicates in the same table. But if a value is present in just 2 of the 3 tables, that also needs to be flagged and there is no need to check for existence in he third table, or check if there are more such values. One value, present in more than one table, and I can stop.
Although id is not unique within any given table, it should be unique across the tables; a union of distinct id should be unique, so:
select id from (
select distinct id from a
union all
select distinct id from b
union all
select distinct id from c) x
group by id
having count(*) > 1
Note the use of union all, which preserves duplicates (plain union removes duplicates).
I would suggest a simple join:
select a.id
from a join
b
on a.id = b.id join
c
on a.id = c.id
limit 1;
If you have a query that uses union or group by (or order by, but that is not relevant here), then you need to process all the data before returning a single row. A join can start returning rows as soon as the first values are found.
An alternative, but similar method is:
select a.id
from a
where exists (select 1 from b where a.id = b.id) and
exists (select 1 from c where a.id = c.id);
If a is the smallest table and id is indexes in b and c, then this could be quite fast.
Try this
select id from
(
select distinct id, 1 as t from a
union all
select distinct id, 2 as t from b
union all
select distinct id, 3 as t from c
) as t
group by id having count(t)=3
It is OK to have duplicates in the same table.
The query can/should exit at the first such occurrence.
SELECT 'OMG!' AS danger_bill_robinson
WHERE EXISTS (SELECT 1
FROM a,b,c -- maybe there is a place for old-style joins ...
WHERE a.id = b.id
OR a.id = c.id
OR c.id = b.id
);
Update: it appears the optimiser does not like carthesian joins with 3 OR conditions. The below query is a bit faster:
SELECT 'WTF!' AS danger_bill_robinson
WHERE exists (select 1 from a JOIN b USING (id))
OR exists (select 1 from a JOIN c USING (id))
OR exists (select 1 from c JOIN b USING (id))
;

Singe SQL Query to retrieve fields from 2 different table.

Trying to explain my Question:
Say, Table A contains ID,Number,Location.
Table B contains Option 1,Option 2.
I want to write a single query that would select ID, Number & Option 1 from the Table A and Table B.
(At present I am doing something like:
SELECT ID FROM [A]
SELECT Option 1 FROM [B]
)
You do this:
SELECT A.ID, A.Number, B.Option1
FROM TableA as A, TableB as B
WHERE A.id = B.id;
This Part sets an alias for the table so that you don't have type in the full table name all the time:
TableA as A
TableB as B
This part is the relationship between table A and B.
WHERE A.id = B.id;
Consider reading SQL table relationships http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/