Hide duplicate SQL values

Asked

Viewed 172 times

0

Well, I’m doing a little approval process project. I need to consult two tables with data that may be the same, but I need this same data don’t show up, in case they get hidden.

Example:

TABELA1         TABELA2
 Col1            Col2
   1               1
   2               4
   3               5

I needed him to return one SELECT unopened the duplicate values.

  • Please put the expected answer to the above example in order to resolve the doubts of those who want to help you.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

3 answers

2

Use the UNION:

SELECT t1.campo1 FROM tabela1 t1
UNION
SELECT t2.campo2 FROM tabela2 t2

1

The important thing is to understand what happens in the consultation. UNION makes the union of two results removing the duplicities.

I remember that UNION will take into account all the return columns of the query. Thus, the UNION query will bring a result only field with distinct Ids.

The command mentioned above (represented below) will make a projection between the two tables and will only relate the items of table 1 that does not contain id = in table 2:

SELECT * FROM #t1 tabela1 CROSS JOIN #t2 tabela2 WHERE (tabela1.id <> tabela2.id);

The return of the above query, assuming your example data would be something like:

   id tb1  id tb2
    1        4
    1        5
    2        1
    2        4
    2        5
    3        1
    3        4
    3        5     

Tabela1 id 1 only designed with different id’s.

0

If I understood correctly your question the SQL expression would be:

SELECT * FROM tabela1 CROSS JOIN tabela2 WHERE (tabela1.col1 <> tabela2.col2);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.