关于SQL中的SELECT DISTINCT的困惑 [英] Confusion about SELECT DISTINCT in SQL
问题描述
当我只选择COLUMN_1时,我在数据库中得到了正确的8行,因为它没有给我重复的行及其所需的内容
When I select COLUMN_1 only I get the right amount of 8 rows in my database because it doesn't give me duplicate rows and its what I want
SELECT DISTINCT COLUMN_1
FROM TABLE
WHERE COLUMN_3=12;
但是当同时选择COLUMN_1和COLUMN_2时,它在数据库中给了我53行,那不是我想要的...
but when select both COLUMN_1, COLUMN_2 it gives me 53 rows in my database and that's not what I want...
SELECT DISTINCT COLUMN_1,COLUMN_2
FROM TABLE
WHERE COLUMN_3=12;
我知道当我选择两个具有与众不同的列
,这将为我提供两列组合的唯一值。 DISTINCT
应用于列的组合,而不仅仅是第一列。所以我的问题是如何同时选择 COLUMN_1
和 COLUMN_2
以便它们给我8行而没有重复? ??因为一旦添加 COLUMN_2
,它就会提供我应该具有的更多行。
I know when I select two columns with distinct
, it will give me unique values for the combination of the two columns. DISTINCT
is applied to the combination of the columns, not just the first column. So my question is how do I select both COLUMN_1
and COLUMN_2
so that they give me 8 rows and no duplicates ???? because as soon as I add COLUMN_2
it gives more rows that I am supposed to have..
推荐答案
您必须确定想要的 column_2
的哪个值。
You have to decide which value for column_2
you want.
一种简单的方法是将分组为
:
SELECT COLUMN_1, min(COLUMN_2)
FROM TABLE
WHERE COLUMN_3=12
GROUP BY COLUMN_1;
这篇关于关于SQL中的SELECT DISTINCT的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!