连接表上的 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:32:08  来源:igfitidea点击:

SQL Distinct value over joined tables

sqljoin

提问by karlstackoverflow

I am trying to get the DISTINCTvalue 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 DISTINCTon the column, it produces the same results because DISTINCTalso takes into account the unique identifier ID. Is there any work around for this to just get the DISTINCTvalue 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