简体   繁体   English

MS Access:根据另一个表字段的多条记录从一个表中选择记录

[英]MS Access: Selecting records from 1 table based on multiple records of another table's field

My question would be: How can I SELECT records of 1 table, based on another table's multiple records with a specific field value?我的问题是:如何根据另一个表的具有特定字段值的多条记录来记录 1 个表的 SELECT 条记录?

So I have 2 tables, both with 3 fields.所以我有 2 个表,都有 3 个字段。 Important to note, that [suti].[id] === [tartalom].[sutiid].需要注意的重要一点是,[suti].[id] === [tartalom].[sutiid]。

My job would be to select all [suti].[nev] and [suti].[tipus] that have both "G" and "To" in [tartalom].[mentes].我的工作是 select 所有 [suti].[nev] 和 [suti].[tipus] 在 [tartalom].[mentes] 中都有“G”和“To”。 However, "G" and "To" are recorded in separate records, and that is why I have a problem with this.但是,“G”和“To”记录在不同的记录中,这就是我对此有疑问的原因。

In this example I have written below, the only record that should be selected is Something2 ||在我下面写的这个例子中,应该选择的唯一记录是 Something2 || tortaszelet. tortaszelet。

How is this achieveable?这是如何实现的? Thank you for your help in advance:)提前谢谢你的帮助:)

Before anybody says, it's not me that did this data structure.之前有人说,这个数据结构不是我做的。 This is what I have, and cannot change it.这就是我所拥有的,无法改变它。

Table 1: suti表 1:suti

id ||编号 || nev ||新|| tipus tipus


1 || 1 || Something1 ||东西1 || torta托尔塔
2 || 2 || Something2 ||东西2 || tortaszelet玉米饼
3 || 3 || Something3 ||东西3 || pite可怜

Table 2: tartalom表 2:塔他洛姆

id ||编号 || sutiid || sutiid || mentes精神


1 || 1 || 1 || 1 || L大号
2 || 2 || 1 || 1 || To
3 || 3 || 2 || 2 || G G
4 || 4 || 2 || 2 || To
5 || 5 || 3 || 3 || G G

All the following is standard SQL.以下均为标准SQL。

Simple aggregation to find the corresponding id:简单聚合找到对应的id:

SELECT sutiid
  FROM tartalom WHERE mentes IN ('G', 'To')
 GROUP BY sutiid
HAVING COUNT(DISTINCT mentes) = 2
;

Now just join (not necessarily a join) this with the first table to obtain that row or rows.现在只需将它与第一个表连接(不一定是连接)以获得该行或多行。

SELECT *
  FROM suti
 WHERE id IN (
     SELECT sutiid
       FROM tartalom
      WHERE mentes IN ('G', 'To')
      GROUP BY sutiid
     HAVING COUNT(DISTINCT mentes) = 2
     )
;

Access doesn't support COUNT(DISTINCT x) , but should handle this, which is also standard SQL: Access 不支持COUNT(DISTINCT x) ,但应该处理这个,这也是标准的 SQL:

SELECT *
  FROM suti
 WHERE id IN (
     SELECT sutiid
       FROM (
           SELECT DISTINCT sutiid AS sutiid, mentes
             FROM tartalom
            WHERE mentes IN ('G', 'To')
            ) xxx
      GROUP BY sutiid
     HAVING COUNT(*) = 2
     )
;
SELECT * FROM suti WHERE id IN ( SELECT suited FROM ( SELECT DISTINCT sutiid AS sutiid, mentes FROM tartalom WHERE mentes IN ('G', 'To') ) xxx GROUP BY sutiid HAVING COUNT(*) = 2 ) ;

Okay I was able to restructure your solution.好的,我能够重组您的解决方案。 Here it is:这里是:

SELECT nev, tipus
 FROM suti, tartalom
  WHERE suti.id=sutiid
   AND (mentes="G" or mentes="To")
  GROUP BY nev, tipus
 HAVING Count(sutiid)=2;

Thank you:)谢谢你:)

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

相关问题 MS ACCESS-根据另一个字段的条件从表中动态检索记录 - MS ACCESS - Dynamically retrieve records from table based on criteria from another field 基于多个条件的表中的MS ACCESS计数记录 - MS ACCESS Counting Records from table based on multiple criteria 根据B中的多条记录从表A中选择记录 - Selecting records from table A based in multiple records in B 根据另一个表中的条件在表中选择记录 - Selecting records in on table based on conditions from another table 从表中选择记录,然后从另一个表中选择记录计数 - Selecting records from a table and then selecting a count of records from another table 根据另一个表的内容在SQL中选择记录 - Selecting records in SQL based on another table's contents 从不在另一个表中的表中选择记录 - Selecting records from a table not in another table SQL基于MS Access中另一个表(一对多)中多个相关记录的值来SQL选择记录的最快方法? - Fastest way to SQL select records based on values of multiple related records in another table (one-to-many) in MS Access? 使用MS Access表单控件将记录从一个表复制到另一个表 - Copy records from one table to another with MS Access form control Clojure从表中选择记录并将这些记录插入另一个表 - Clojure Selecting records from a table and inserting those records into another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM