从多个表中选择,没有重复

时间:2013-08-21 05:38:20

标签: sql oracle

我有来自5个表的复杂select语句, 我想避免从其中一个表中具有相同字段值的重复行。

SELECT  role_enname
          ,lu_app_operations.operation_enname
          , lu_applications.app_enname
          , lu_app_oper_groups.oper_group_enname 
FROM app_operation_roles
      ,roles
      ,lu_app_operations
       ,lu_applications
        ,lu_app_oper_groups
WHERE app_operation_roles.role_id=roles.role_id
AND app_operation_roles.app_operation_id=lu_app_operations.app_operation_id
AND roles.app_id=lu_applications.app_id
AND lu_app_operations.app_oper_group_id=lu_app_oper_groups.app_oper_group_id
AND app_operation_roles.retired  =0
AND roles.retired                =0 

在此查询中,我不会根据lu_app_operations.operation_enname

显示重复行

4 个答案:

答案 0 :(得分:0)

使用DISTINCT关键字:

SELECT DISTINCT role_enname, <rest of your query>

答案 1 :(得分:0)

使用distinct关键字如下

SELECT  DISTINCT role_enname,lu_app_operations.operation_enname, lu_applications.app_enname,
lu_app_oper_groups.oper_group_enname 
FROM app_operation_roles,roles,lu_app_operations,lu_applications,lu_app_oper_groups
WHERE app_operation_roles.role_id=roles.role_id
AND app_operation_roles.app_operation_id=lu_app_operations.app_operation_id
AND roles.app_id=lu_applications.app_id
AND lu_app_operations.app_oper_group_id=lu_app_oper_groups.app_oper_group_id
AND app_operation_roles.retired  =0 AND roles.retired                =0 

答案 2 :(得分:0)

也许您可以使用Distinct和Group by来获取结果,但请记住,要加入多个表,最好还是使用JOIN代替AND

SELECT  DISTINCT role_enname
      ,lu_app_operations.operation_enname
      , lu_applications.app_enname
      , lu_app_oper_groups.oper_group_enname 
FROM app_operation_roles
,roles
,lu_app_operations
,lu_applications
,lu_app_oper_groups
WHERE app_operation_roles.role_id=roles.role_id
AND app_operation_roles.app_operation_id=lu_app_operations.app_operation_id
AND roles.app_id=lu_applications.app_id
AND lu_app_operations.app_oper_group_id=lu_app_oper_groups.app_oper_group_id
AND app_operation_roles.retired  =0
AND roles.retired                =0 
GROUP BY role_enname, 
lu_app_operations.operation_enname,
lu_applications.app_enname,
lu_app_oper_groups.oper_group_enname 

答案 3 :(得分:0)

SELECT  distinct role_enname,lu_app_operations.operation_enname, lu_applications.app_enname,lu_app_oper_groups.oper_group_enname 
FROM app_operation_roles,roles,lu_app_operations,lu_applications,lu_app_oper_groups
WHERE app_operation_roles.role_id=roles.role_id
AND app_operation_roles.app_operation_id=lu_app_operations.app_operation_id
AND roles.app_id=lu_applications.app_id
AND lu_app_operations.app_oper_group_id=lu_app_oper_groups.app_oper_group_id
AND app_operation_roles.retired =0
AND roles.retired =0