【问题标题】:SQL Inner join in MS Access 2007MS Access 2007 中的 SQL 内部连接
【发布时间】:2011-11-17 07:01:06
【问题描述】:

我在 Access 数据库中遇到内部联接问题。我有两个表,每个表有两列。两个表中一个是 MID,另一个是 DOB。第一个表名为maintable,其他为under5。我想在将 under5 表与 maintable 匹配后检索所有这些记录,所以我使用了内连接。问题是有一个 MID 10106,其在 under5 表中的频率为 2,在 maintable 中为 5,但运行查询后返回的 MID(10106) 的数量为 10(我猜预期的行为应该是 2),这真的很荒谬.请帮我解决这个问题。 这是我的查询:

SELECT maintable.MID, maintable.DOB, Under5.MID, Under5.DOB
FROM under5
INNER JOIN maintable ON under5.MID=maintable.MID;

【问题讨论】:

    标签: sql database ms-access join


    【解决方案1】:

    行为非常好。

    主表中的每一行都将与内表中的每一行匹配。因为主表有 5 行,内表有 2 行 5*2 = 10 行

    Can there be any other way so that i could only retrieve those MID and DOB 
    that is present in under5 table after matching it with maintable.
    

    试试这个

    SELECT distinct Under5.MID, Under5.DOB 
    FROM under5, MainTable
    where under5.mid = mainTable.mid
    

    【讨论】:

    • 能否有其他方法让我只能在与 maintable 匹配后检索 under5 表中存在的那些 MID 和 DOB。
    • 能否有其他方法让我只能在与 maintable 匹配后检索 under5 表中存在的那些 MID 和 DOB。
    • 我尝试了 Distinct 子句,但以同样的问题结束。不知道为什么访问会这样
    • @user510686 请查看上面的编辑答案,看看它是否有效?
    【解决方案2】:

    您正在描述semijoin,例如试试:

    SELECT Under5.MID, Under5.DOB
      FROM under5
     WHERE EXISTS (
                   SELECT * 
                     FROM maintable 
                    WHERE maintable.MID = under5.MID
                  );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      相关资源
      最近更新 更多