我需要从我的数据库中提取一些数据,但问题是我从数据库中获取了重复的值,这是我不想要的。
用户名多次存入数据库。
我尝试使用 DISTINCT,但没有用。
谁能告诉我如何从数据库中只提取一次用户名?
代码:
PreparedStatement preparedStatement = connect
.prepareStatement("SELECT DISTINCT username, score, name from score order by score desc limit 10");
最佳答案
使用 group by
而不是 distinct
。 Select distinct
适用于所有被选中的列:
SELECT username, max(score) as score, max(name) as name
from score
group by username
order by score desc
limit 10;
关于java - 从 MYSQL 数据库中选择 DISTINCT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24221721/