SQL使用Distinct返回重复记录

时间:2017-12-06 09:48:50

标签: mysql sql hibernate

我正在尝试执行以下查询。我期待不应该有重复。但在结果集中我得到重复记录。我是sql的新手。

SELECT DISTINCT pr.id as ruleid, pr.alias as rulename,grp.id as groupId, grp.alias, fgp.filterGroupType FROM  ProcessingService s    INNER JOIN s.fileGroupingParameter fgp ON fgp.status =:fileGroupStatus INNER JOIN s.svcPathList path ON path.status =:pathStatus  INNER JOIN path.policy p  ON p.status =:policyStatus  INNER JOIN p.policyGroupRelSet pgr  INNER JOIN pgr.group grp ON grp.status =:groupStatus  INNER JOIN grp.policyGroupRuleRelSet rr  INNER JOIN rr.policyRule pr  ON pr.status =:ruleStatus  INNER JOIN pr.policyActionSet pa ON pa.status =:actionStatus WHERE s.id in (:idList) AND pa.action =:actionType AND s.status =:serviceStatus AND fgp.filterGroupType ='Rulewise' AND pr.category =:reasonCategory AND pr.severity =:reasonSeverity

我如何根据我的要求获得不同的记录。

我得到的结果集是

"policy rule1"   
"policy rule1"  
"policy rule12"  
"policy rule12"

我希望:

"policy rule1"  
"policy rule12" 

作为输出。

1 个答案:

答案 0 :(得分:0)

Distinct适用于整个列集,不仅适用于一列。如果您在其中一列中获得重复项,则意味着您在集合中的某些其他列中具有唯一值。您必须决定是否需要这些唯一值。 您必须根据某些逻辑删除不需要的列或选择所需的行。