寻找一种不使用SQL查询显示欺骗行的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了寻找一种不使用SQL查询显示欺骗行的方法相关的知识,希望对你有一定的参考价值。

SELECT 
    AEC.gwd_people.id_people,
    AEC.gwd_people.uid_people, 
    AEC.gwd_people.cod_people,
    AEC.gwd_people.name_people, 
    AEC.gwd_people.surname_people,
    AEC.gwd_people.email, 
    AEC.gwd_people.people_status, 
    AEC.gwd_people.people_type,
    AEC.gwd_people.facility_reference,
    AEC.gwd_people.sc_id_sap,
    AEC.gwd_people.c_id_sap,
    AEC.gwd_people.descr_people, 
    AEC.gwd_people.cod_sector,
    AEC.gwd_people.descr_sector,
    AEC.gwd_people.cod_org_sector,
    AEC.gwd_people.descr_org_sector,
    AEC.gwd_people.cod_company,
    AEC.gwd_people.descr_company, 
    AEC.gwd_people.cod_company_sap,
    AEC.gwd_people.cod_department,
    AEC.gwd_department.descr_department,
    AEC.gwd_people.cod_subdepartment,
    AEC.gwd_people.descr_subdepartment, 
    AEC.gwd_people.cod_cdc,
    AEC.gwd_cost_center.descr_cdc,
    AEC.gwd_people.cod_category_job,
    AEC.gwd_people.descr_category_job,
    AEC.gwd_people.cod_people_job,
    AEC.gwd_people.descr_people_job, 
    AEC.gwd_people.cod_position,
    AEC.gwd_people.descr_position,
    AEC.gwd_people.uohr,
    AEC.gwd_people.qual_contract,
    AEC.gwd_people.level_position,
    AEC.gwd_people.cod_manager, 
    AEC.gwd_people.cod_validator,
    AEC.gwd_people.cod_country,
    AEC.gwd_people.descr_country,
    AEC.gwd_people.cod_region_area,
    AEC.gwd_people.descr_region_area,
    AEC.gwd_people.descr_city, 
    AEC.gwd_people.descr_site,
    AEC.gwd_people.address_1,
    AEC.gwd_people.address_2,
    AEC.gwd_people.descr_building,
    AEC.gwd_people.descr_room,
    AEC.gwd_people.validity_date, 
    AEC.aec_workstation.cod_workstation,
    AEC.aec_workstation.geometry,
    AEC.aec_workstation.drawing,
    AEC.gwd_people.tax_code,
    AEC.gwd_people.phone_1,
    AEC.gwd_people.phone_2,
    AEC.gwd_people.phone_3, 
    AEC.gwd_people.phone_4,
    AEC.gwd_people.ext_email_1,
    AEC.gwd_people.flagvip,
    AEC.gwd_people.hiring_date,
    AEC.gwd_people.cease_date,
    AEC.gwd_people.cid_resp_liv_1,
    AEC.gwd_people.cid_resp_liv_2, 
    AEC.gwd_people.id_resp,
    AEC.gwd_people.descr_resp, 
    AEC.gwd_people.id_ref,
    AEC.gwd_people.descr_ref, 
    AEC.gwd_people.descr_ext_people,
    AEC.gwd_people.ext_email_2,
    AEC.gwd_people.descr_sede, 
        (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NULL
          THEN AEC.gwd_people.idplan
          ELSE NULL
          END) AS idplan,
        (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL 
          THEN SUBSTRING(AEC.aec_workstation.cod_workstation, 5, 7)
           ELSE NULL
           END) AS idplan_wrkst,
        (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NULL 
          THEN AEC.view_iam_r_unitp_building.IDEDIFICIO
           ELSE NULL
           END) AS cod_building,
        (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL 
          THEN SUBSTRING(AEC.aec_workstation.cod_workstation, 5, 3)
           ELSE NULL
           END) AS cod_building_wrkst,
        (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL 
          THEN AEC.aec_workstation.id_room
           ELSE NULL
           END) AS id_room_wrkst,
        (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL
          THEN AEC.aec_workstation.id_room
           ELSE NULL
           END) AS id_room_wrkst2
FROM AEC.gwd_people
 LEFT OUTER JOIN AEC.view_iam_r_unitp_building ON 
 AEC.view_iam_r_unitp_building.IDUNITPROD = AEC.gwd_people.cod_sector
 LEFT OUTER JOIN AEC.aec_r_workstation_people ON AEC.gwd_people.cod_people = 
 AEC.aec_r_workstation_people.cod_people
 LEFT OUTER JOIN AEC.aec_workstation ON AEC.aec_workstation.cod_workstation 
 = AEC.aec_r_workstation_people.cod_workstation
 LEFT OUTER JOIN AEC.gwd_department ON AEC.gwd_department.cod_department = 
 AEC.gwd_people.cod_department
 LEFT OUTER JOIN AEC.gwd_cost_center ON AEC.gwd_cost_center.cod_cost_center 
 = AEC.gwd_people.cod_cdc

这是我的查询,我使用的是SQL Server 13,它返回6752行,其中44行被欺骗。我已经尝试了我所知道的一切,以避免显示那些被骗的条目,但我没有想法,所以我正在寻找一些有用的提示:-)最大的问题之一是所有领域都是必要的,所以我不能摆脱导致SELECT DISTINCT问题的“AEC.aec_workstation.geometry”。

答案

从第一个表中找到一个返回重复行的PK值并从以下查询开始:

SELECT 
    COUNT(1)
FROM 
    AEC.gwd_people
WHERE
    AEC.gwd_people.PrimaryKeyColumn = 'SomeValue'

现在开始逐个添加连接,每次检查COUNT(1)的结果:

SELECT 
    COUNT(1)
FROM 
    AEC.gwd_people
    LEFT OUTER JOIN AEC.view_iam_r_unitp_building ON AEC.view_iam_r_unitp_building.IDUNITPROD = AEC.gwd_people.cod_sector
WHERE
    AEC.gwd_people.PrimaryKeyColumn = 'SomeValue'

然后...

SELECT 
    COUNT(1)
FROM 
    AEC.gwd_people
    LEFT OUTER JOIN AEC.view_iam_r_unitp_building ON AEC.view_iam_r_unitp_building.IDUNITPROD = AEC.gwd_people.cod_sector
    LEFT OUTER JOIN AEC.aec_r_workstation_people ON AEC.gwd_people.cod_people = AEC.aec_r_workstation_people.cod_people
WHERE
    AEC.gwd_people.PrimaryKeyColumn = 'SomeValue'

直到你看到你不期望它的行数量跳跃。你最有可能:

  • 不考虑可以预期重复的行。
  • 缺少表上的另一个连接列。
  • 在表上有重复的行。

......或者这些的组合。

另一答案

你的桌面设计让人有点难以理解他们的关系。这就是我的样子:

gwd_department  {1:n}  gwd_people
gwd_people      {m:n}  aec_workstation
gwd_people      {m:n}  view_iam_r_unitp_building
gwd_people      {?:n}  gwd_cost_center

因此,对于链接到3 aec_workstations和4 view_iam_r_unitp_buildings的人,您将生成3 x 4 = 12个结果行。 aec_workstation和view_iam_r_unitp_building之间没有进一步的关系吗?如果没有,那么为什么要在查询中将它们合并?

我不知道cod_cdc是否应该是cod_cost_center的缩写或者是不同的东西。如果这也是m:n关系,那么你使用与aec_workstation和view_iam_r_unitp_building相关的gwd_cost_center再次做同样的事情。

话虽如此:要么添加缺少的标准,要么问自己你想要选择什么。

以上是关于寻找一种不使用SQL查询显示欺骗行的方法的主要内容,如果未能解决你的问题,请参考以下文章

使用地理跟踪和 SQL 查询

如何在当前行的开头显示垂直规则?

我正在寻找一种从字符串构建 IQueryable 查询的方法

一种不太合规的PreparedStatement使用方式

java源代码审计-sql注入

使用 C# 检测破坏性 SQL 查询