user3702643
user3702643

Reputation: 1495

Select DISTINCT from MYSQL database

I need to extract some data from my database, but the problem is that Im getting repeated values from the database, which I do not want.

The username is stored into the database multiple times.

I tried using DISTINCT but it did not work.

Can anyone tell me how to extract the username from the database only once?

Code:

PreparedStatement preparedStatement = connect
            .prepareStatement("SELECT DISTINCT username, score, name from score order by score desc limit 10");

Upvotes: 1

Views: 498

Answers (2)

hansmei
hansmei

Reputation: 701

Remove the score and name from your query.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269553

Use group by instead of distinct. Select distinct applies to all columns being selected:

SELECT username, max(score) as score, max(name) as name
from score
group by username
order by score desc
limit 10;

Upvotes: 3

Related Questions