Select from different tables [closed]

0

How to do a select from different tables? Type, check if there is value in one, if not, check the other.

    
asked by anonymous 09.04.2017 / 20:46

1 answer

2

Although the question does not make the context explicit, I will try to respond as generically as possible (although for me it is almost impossible not to think about PostgreSQL. :-))

Let's say we have the following scheme

CREATE TABLE Table1 (
    code char(5) CONSTRAINT firstkey PRIMARY KEY);

CREATE TABLE Table2 (
    code char(5) CONSTRAINT secondkey PRIMARY KEY);

And the following inserts:

INSERT INTO Table1 VALUES ('1');
INSERT INTO Table1 VALUES ('2');
INSERT INTO Table1 VALUES ('3');

INSERT INTO Table2 VALUES ('1');
INSERT INTO Table2 VALUES ('2');
INSERT INTO Table2 VALUES ('3');
INSERT INTO Table2 VALUES ('4');
INSERT INTO Table2 VALUES ('5');

Let's say we need to retrieve the code = '5', but we do not know what tabala it is, so a path would be:

SELECT code FROM Table2 WHERE code='5' and code NOT IN 
    (SELECT code FROM Table1 where code='5')  

If I understood correctly the question, this is a path.

    
09.04.2017 / 22:18