简体   繁体   English

如何显示一个表中不存在于另一表中的记录

[英]How to show records from one table that do not exist in another table

This is a little more complicated than the usual how do I display records from one table which are not in the other.这比通常如何显示一个表中不在另一个表中的记录要复杂一些。

Rather, each record from one table should appear 'x' number of times in the other.相反,一个表中的每条记录都应该在另一个表中出现“x”次。

The table definitions and data are as follows:表定义和数据如下:

CREATE TABLE `time_code` (
  `ID` bigint NOT NULL AUTO_INCREMENT,
  `AREA_ID` bigint NOT NULL,
  `TIME_CODE` varchar(10) NOT NULL,
  `DESCRIPTION` varchar(255) NOT NULL,
  `FISCAL_YEAR` bigint NOT NULL,
  PRIMARY KEY (`ID`)
  )

Id, Area, Timecode, Description, Fiscal Year
1, 51, 101, "Project A", 2022
2, 51, 102, "Project B", 2022
CREATE TABLE `schedule_data` (
  `ID` bigint NOT NULL AUTO_INCREMENT,
  `AREA_ID` bigint NOT NULL,
  `TERRITORY_ID` bigint NOT NULL,
  `GROUP_ID` bigint NOT NULL,
  `TIMECODE_ID` bigint NOT NULL,
  `CALENDAR_MONTH` bigint NOT NULL,
  `FISCAL_YEAR` bigint not null,
  PRIMARY KEY (`ID`)
  )
Id, Area, Territory, Group, Timecode ID, Month, Fiscal Year
1, 51, 52, 53, 1, 2, 2022
2, 51, 52, 54, 1, 2, 2022
3, 51, 52, 55, 1, 2, 2022
4, 51, 52, 53, 2, 2, 2022

In this example, time code 101 appears in each of the groups (53, 54, and 55).在此示例中,时间码 101 出现在每个组(53、54 和 55)中。 Time code 102 appears only in group 53. It is missing from groups 54 and 55.时间码 102 仅出现在第 53 组中。它在第 54 和 55 组中缺失。

Each timecode must appear in the schedule_data table for each group (3 times, but it could vary depending upon the number of groups)每个时间码必须出现在每个组的 schedule_data 表中(3 次,但可能因组数而异)

How do I write a query that tells me which groups are missing time codes?如何编写一个查询来告诉我哪些组缺少时间码?

Time codes apply to an entire area and the code must exist exactly once for each group...even if the values being tracked are zero.时间代码适用于整个区域,并且每个组的代码必须仅存在一次......即使被跟踪的值为零。

Edit to add org code table and sample records:编辑以添加组织代码表和示例记录:

// adjacency list model
CREATE TABLE `organization_map` (
  `ID` bigint NOT NULL AUTO_INCREMENT,
  `ORG_CODE` bigint NOT NULL,
  `NAME` varchar(255) NOT NULL,
  `PARENT_ID` bigint NOT NULL,
  `FISCAL_YEAR` bigint NOT NULL,
  PRIMARY KEY (`ID`)
)
Id, Org Code, Name, Parent Id, Fiscal Year
51, 1, "Area 1", 0, 2022
52, 1, "Territory 1", 51, 2022
53, 5, "Group 1", 52, 2022
54, 6, "Group 2", 52, 2022
55, 7, "Group 3", 52, 2022
56, 2, "Territory 2", 51, 2022
57, 14, "Group 4", 56, 2022
58, 15, "Group 5", 56, 2022

You can use NOT EXISTS and a correlated subquery which aggregates and checks for the sum() of matching time codes in a HAVING clause.您可以使用NOT EXISTS和相关子查询,它聚合并检查HAVING子句中匹配时间码的sum()

SELECT *
       FROM `time_code` AS tc
       WHERE NOT EXISTS (SELECT ''
                                FROM `schedule_data` AS sd
                                GROUP BY sd.`group`
                                HAVING sum(sd.`timecode_id` = tc.`id`) = 0);

UPDATE, solution found更新,找到解决方案

Created a view of the schedule data to include the organization info for easier query.创建计划数据视图以包含组织信息以便于查询。 Then wrote a query to represent the time codes down to the group level.然后写了一个查询来表示时间码到组级别。 Next, used a not in sub-query to find the time codes that are not present in the view.接下来,使用 not in 子查询来查找视图中不存在的时间码。

select
    a.id as area_id, t.id as territory_id, g.id as group_id, tc.time_code
from 
    time_code tc
left join
    organization_map a on a.id = 51
left join
    organization_map t on t.id = 52
left join
    organization_map g on g.id in
    (
        58, 59, 60, 61, 62, 63, 64
    )
where
    tc.FISCAL_YEAR = 2022 and
    tc.TIME_CODE not in
    (
        select
            v.time_code
        from
            schedule_data_view v
        where
            v.calendar_month = 10 and
            v.FISCAL_YEAR = 2021 and
            v.AREA_ID = a.id and
            v.TERRITORY_ID and
            v.group_id = g.id
    )
group by
    a.id, t.id, g.id, tc.TIME_CODE;

Basically you can cross join the groups and the codes to get all possible combinations.基本上,您可以交叉加入组和代码以获得所有可能的组合。 Then you can use NOT EXISTS and a correlated subquery to find the combinations that are not in the schedule.然后,您可以使用NOT EXISTS和相关子查询来查找不在计划中的组合。

SELECT om.id organization_map,
       tc.id time_code
       FROM organization_map AS om
            CROSS JOIN time_code AS tc
       WHERE NOT EXISTS (SELECT *
                                FROM schedule_data AS sd
                                WHERE sd.group_id = om.id
                                      AND sd.timecode_id = tc.id);

I'm not sure about those other columns like fiscal_year , calendar_month etc.. I think that they are just redundant and shouldn't even exist but I don't know what exactly you're modeling here.我不确定其他列,如fiscal_year年、 calendar_month等。我认为它们只是多余的,甚至不应该存在,但我不知道你在这里建模的到底是什么。 So they might have their places and be part of the keys.所以他们可能有自己的位置并成为钥匙的一部分。 In the latter case, you may need to extend the query using these key columns as well.在后一种情况下,您可能还需要使用这些键列来扩展查询。

暂无
暂无

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

相关问题 从一个表中选择 Laravel 5.1 中另一个表中不存在的所有记录 - Select all records from one table that do not exist in another table in Laravel 5.1 如何在一个表中找到确实存在但基于日期的记录? - How do I find records in one table that do exist in another but based on a date? 从一个表中查找另一个表中不存在的记录 - Find records from one table which don't exist in another 如何将记录从一张表更新到另一张表 - how to update the records from one table to another 如何从一个表中选择所有在mysql中两个其他表的联合中不存在的记录 - How to select all records from a table that do not exist in a union of two another tables in mysql MySQL显示存在于一个表中但不存在于另一个表中的行 - MySQL Show rows that exist in one table but not it another 我们如何从一个表到另一个表插入“来自多个记录的数据”? - How do we insert “data from more than one records”, from one table to another? 从一个表中查找不存在于另一个表中并在第三个表中忽略的记录 - Find records from one table which don't exist in another and ignored in a third table 我如何检查一个表中是否存在会话ID,而另一表中是否存在该ID,则回显错误或成功代码 - how do i check if session ID exist from one table exist in another table and echo out error or success code 从表中删除另一个表中不存在的所有记录 - Removing all records from a table that don't exist in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM