简体   繁体   English

如何使用 sql 查询 (DISTINCT) 检索唯一的数据输出?

[英]How to retrieve a unique data output using sql query (DISTINCT)?

The data set is like this:数据集是这样的:

Table A表A

==type===|| ==类型====|| ===score==== ===得分====

Two columns type ands score ,两列typescore

and Table B和表 B

==== id ==== || ==== id ==== || ==== data ==== ====数据====

Two columns as well id and data .两列以及iddata

So, the details are as follows:因此,详细信息如下:

the id in table B should be an INNER JOIN for the type in table A.表 B 中的id应该是表 A 中type的 INNER JOIN。

Now an example case of data in each table,现在每个表中的数据示例,

table A:表一:

type == A, score = 75.
type == A, score = 80.   
type == B, score = 65. 
type == B, score = 75.

and table B:和表B:

id == A, data = "Good score"
id == B, data = "Not bad..."

I want the query to only select the data that is unique and distinct to it's own category type ie if A it should be score of 80 and not score of 75 as 75 is unique to type B's score.我希望查询只选择对其自己的类别类型唯一且不同的数据,即如果 A 应该是 80 分而不是 75 分,因为 75 是类型 B 的唯一分数。

Hope that helps with the problem dilemma, and I would appreciate any guidance to this question.希望有助于解决问题困境,我将不胜感激对此问题的任何指导。

You can use NOT EXISTS and a correlated subquery to check for the non existence of rows with the same score but different type.您可以使用NOT EXISTS和相关子查询来检查是否不存在具有相同分数但类型不同的行。

SELECT a1.type,
       a1.score,
       b1.data
       FROM a a1
            INNER JOIN b b1
                       ON b1.id = a1.type
       WHERE NOT EXISTS (SELECT *
                                FROM a a2
                                WHERE a2.score = a1.score
                                      AND a2.type <> a1.type);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM