SELECT DISTINCT仍显示重复项 [英] SELECT DISTINCT still showing duplicates
问题描述
是的,关于SO的问题有上千个,但我已经搜索了半个小时,但尚未找到解决方案.
Yes, there's a thousand questions about this on SO, but I've been searching for half an hour and I've yet to find a solution.
所以,我有一个像这样的表:
So, I've a table like this:
这是我的查询:
SELECT DISTINCT rengasID,leveys FROM renkaat ORDER BY leveys ASC
这是我得到的结果:
如果您有这个主意,我会在其中填充一个选择字段,但是它仍然有重复项.
If you get the idea, I'm populating a select field with it, but it still has duplicates.
我在做什么错了?
推荐答案
如果要使用不同的levey
,只需选择该字段即可:
If you want distinct levey
s, just choose that field:
SELECT DISTINCT leveys
FROM renkaat
ORDER BY leveys ASC
rengasid
在每一行上都有不同的值.
The rengasid
has a different value on each row.
distinct
子句适用于所有返回的列,无论括号如何.
The distinct
clause applies to all the columns being returned, regardless of parentheses.
如果结果中需要regasid
,请使用group by
:
If you need the regasid
in the result, then use group by
:
select leveys, min(regasid) as regasid
from renkaat
group by leveys
order by leveys asc;
这给出了第一个ID.如果需要所有它们,可以使用group_concat()
将它们放在列表中.如果每行都需要一个单独的ID,那么您就有重复项了.
This gives the first id. If you need all of them, you can get them in a list using group_concat()
. If you need a separate id on each row, well, then you have duplicates.
这篇关于SELECT DISTINCT仍显示重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!