Batara Yamadipati
Batara Yamadipati

Reputation: 13

Select only 1 from 2 tables when value is duplicate

I have table information like this:

Table A:

001, YANDI, Jl damar
002, YAYA,  jl Selir
004, Nana , jl manggis

Table B:

003, maman, jl sehat
001, Yandi, jl damar

I want:

001, Yandi, jl damar
002, yaya,  jl selir
003, Maman, Jl sehat
004, Nana,  jl manggis

i am using

select distinct a.id, b.name from a as a join b as b on a.id= b.id 

but does not working

Upvotes: 0

Views: 52

Answers (2)

Laxmi
Laxmi

Reputation: 3810

SELECT
  id,
  name
FROM Table_3
UNION
SELECT
  id,
  name
FROM Table_4;

'*' means "all columns" in the table will shown in the output.

If you want to display the specific field in the output, then

SELECT
  column_name1,
  column_name2,
  column_name3
FROM table_name;

In Sq-server,

this can be used to find difference between Table_A and Table_B

(   SELECT * FROM Table_A
    EXCEPT
    SELECT * FROM Table_B)  
UNION ALL
(   SELECT * FROM Table_B
    EXCEPT
    SELECT * FROM Table_A) 

Upvotes: 0

davidwebster48
davidwebster48

Reputation: 580

Try something like this:

SELECT * FROM TABLE_A
UNION
SELECT * FROM TABLE_B

The "union" forces the results to be unique. If you didn't want unique results then you'd use "union all".

Upvotes: 1

Related Questions