连接表上的 SQL Distinct 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9425276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Distinct value over joined tables
提问by karlstackoverflow
I am trying to get the DISTINCT
value of one column in a table. This column however is INNER JOINED from another table through an id
.
我正在尝试获取DISTINCT
表中一列的值。但是,此列是通过id
.
When I try to use the DISTINCT
on the column, it produces the same results because DISTINCT
also takes into account the unique identifier ID. Is there any work around for this to just get the DISTINCT
value of a column from a joined table???
当我尝试DISTINCT
在列上使用时,它会产生相同的结果,因为DISTINCT
也考虑了唯一标识符 ID。是否有任何解决方法可以仅从DISTINCT
连接表中获取列的值???
EG.
例如。
SELECT val1, b.val2, val3
FROM TABLE 1
JOIN (SELECT DISTINCT val2
FROM TABLE 2) AS b ON val1 = b.val2
采纳答案by karlstackoverflow
To provide my solution: I ended up using a nested distinct through a join and all the unnested values (all 20+) of them had to be wrapped around a MIN(x), seeing as those values didnt matter that much, so long as only one distinct value was returned.
提供我的解决方案:我最终通过连接使用了嵌套的不同值,并且所有未嵌套的值(全部 20 个以上)都必须包裹在 MIN(x) 周围,因为这些值并不重要,只要只返回了一个不同的值。
回答by northpole
Try throwing in a GROUP BY instead of a DISTINCT:
尝试使用 GROUP BY 而不是 DISTINCT:
SELECT val1
, b.val2
, val3
FROM TABLE 1
JOIN (SELECT val2
FROM TABLE 2 GROUP BY val2) AS b ON val1 = b.val2