JavaUser
JavaUser

Reputation: 26384

SQL join: avoid duplicate entries while joining two tables?

I have two tables, A and B, and I am trying to select rows from A based on a join query. I am getting some records from these tables, but there are some duplicate entries. How should I filter this query to avoid duplicate rows? For now, I'm doing it via Java code by putting these into a HashSet.

Upvotes: 2

Views: 42769

Answers (2)

tutorial-computer.com
tutorial-computer.com

Reputation: 86

You must explain table structure but I can understand your question. You may use this query:

select a.atribute1, a.atribute2, b.atribut1, b.atribut2
  from a inner join b
    on a.primarykey = b.foreign_key
 where a.atribute = ""
 group by a.atribute2, b.atribute2

You can use a.atribute = "" for validation.

Use group by a.atribute2, b.atribute2 if you want the row in one group of this attribute.

Upvotes: 4

Jonathan Leffler
Jonathan Leffler

Reputation: 755006

The keyword DISTINCT is used to eliminate duplicate rows from a query result:

SELECT DISTINCT ...
  FROM A
  JOIN B ON ...

However, you can sometimes (possibly even 'often', but not always) avoid the need for it if the tables are organized correctly and you are joining correctly.

To get more information, you are going to have to ask your question more clearly, with concrete examples.

Upvotes: 8

Related Questions