简体   繁体   English

SQL 仅选择第二个表中的行,包含从第一个表收到的确切结果

[英]SQL selecting only rows from 2nd table, containing exact results received from 1st table

The answer must be rather simple, but I stuck with it for a while.答案一定很简单,但我坚持了一段时间。 2 tables 2张桌子

  1. BUILDING-TYPE Some types of work are located in some buildings BUILDING-TYPE 某些类型的工作位于某些建筑物中

  2. WORKER-TYPE Worker can do some types of work WORKER-TYPE Worker 可以做一些类型的工作

TYPE_BLD_ID TYPE_BLD_ID BLD BLD TYPE类型
2 2 1 1 2 2
6 6 2 2 5 5
7 7 2 2 6 6
8 8 3 3 6 6
9 9 4 4 2 2
4 4 5 5 3 3
3 3 5 5 4 4
5 5 5 5 7 7
13 13 6 6 1 1
14 14 6 6 7 7
12 12 7 7 5 5
WRK_TYPE_ID WRK_TYPE_ID WORKER工人 TYPE类型
0 0 0 0 0 0
5 5 1 1 3 3
4 4 1 1 4 4
6 6 1 1 7 7
7 7 2 2 2 2
11 11 2 2 4 4
10 10 2 2 5 5
9 9 2 2 6 6
12 12 2 2 7 7
15 15 3 3 5 5
14 14 3 3 6 6
19 19 3 3 7 7
17 17 4 4 1 1
16 16 4 4 2 2
18 18 4 4 7 7

WHEN I select eg Building #4, I expect getting Worker #1, who can handle types of work 1&7.当我 select(例如 4 号楼)时,我希望得到 1 号工人,他可以处理 1 和 7 类工作。 It looks rather stupidly simple, but它看起来相当简单,但是

  1. I select types of work from the building: SELECT TYPE FROM TYPES_IN_BUILDING WHERE BLD = 1; I select 工作类型从建筑: SELECT TYPE FROM TYPES_IN_BUILDING WHERE BLD = 1; Get 1 and 7得到 1 和 7
  2. NEXT I do SELECT WORKER FROM WORKER_TYPES WHERE TYPE IN (1,7);接下来我SELECT WORKER FROM WORKER_TYPES WHERE TYPE IN (1,7);

And, obviously receive all workers, who can do 1 and 7. But I want to get ONLY WORKERS, who has BOTH types, 1 AND 7.而且,显然接收所有可以做 1 和 7 的工人。但我只想获得同时具有 1 和 7 类型的工人。

The issue in the question - JOINs in the SQL version, used in my system, something extremely outdated.问题中的问题 - 在我的系统中使用的 SQL 版本中的 JOINs 非常过时。

THE FOLLOWING WUERY DID NOT WORK:以下 WUERY 无效:

''' '''

SELECT * FROM WORKER W
WHERE W.WORKER IN (SELECT WA.WORKER
    FROM WORKER_ATYPE WA
    WHERE WA.ATYPE IN 
    (SELECT AB.ATYPE FROM ATYPE_IN_BUILD AB WHERE AB.BLD = 6)
    GROUP BY WA.WORKER
    HAVING COUNT(DISTINCT ATYPE) = 
        (SELECT COUNT(ATYPE) FROM TYPE_IN_BUILD WHERE BLD = 6)
 ;

''' '''

You want the worker types to be a superset of the building types.您希望工人类型成为建筑类型的超集。 You can do this using:您可以使用以下方法执行此操作:

select w.worker
from (select b.*, count(*) over (partition by building) as cnt
      from building b
     ) b join
     workers w
     on b.type = w.type
where b.building = 1
group by w.worker, b.cnt
having count(*) = b.cnt;

This matches workers to the building based on types.这会根据类型将工人与建筑物匹配。 Only rows that match are included.仅包含匹配的行。 Then, the workers are aggregated and a worker matches if the total number of rows matches the number of types for the building.然后,如果总行数与建筑物的类型数匹配,则聚合工人并匹配工人。

暂无
暂无

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

相关问题 仅当第 2 个表中不存在记录时,才将第 1 个表中的记录插入到第 2 个表中 - Insert records from 1st table to 2nd table only when the record is not present in the 2nd table 仅存在表的第一个数据库中的第二个数据库中的sql更新数据 - sql update data in 2nd db from 1st db only where table exists 将数据从第三张表链接到第一张表和第二张表 - Link data from third table to a 1st and a 2nd table 如何在 SQL 服务器中从第一个和第二个表中获取匹配记录,并且仅从第一个表中获取不匹配记录,该服务器已加入 1 个字段 - How to get a a matching records from 1st and 2nd table and only non matching records from 1st table in SQL Server having joined by 1 field 如何从第一个表中返回所有列,而从第二个表中只返回一列 - How to return all columns from 1st table and only 1 column from 2nd table SQL查询根据第一个表的输入从第二个表中获取字段 - SQL query to get fields from 2nd table based on input from 1st 如何连接两个SQL表,将第二个表中的值放入第一个表中的列? - How can I join two SQL tables, putting a value from the 2nd table into a column in the 1st? 需要SQL查询帮助-第一个表中的多行应与第二个表中的多个表匹配 - SQL Query help needed - Multiple rows in 1st table should match to multiple table in 2nd table 如何从第二个表中显示2个不同的ID从第一个表中显示ID - How to show id from 1st table from 2 different ids from 2nd table 从第一张表中选择数据并获得第二张表的字段,其中表1中有2列 - select data from 1st table and get the 2nd table's field for 2 columns in table 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM