2

我在使用 sql 查询时遇到了一些问题。以下查询返回权限结果。它返回表中男性、说瑞典语并且在第 4 组中的成员。

SELECT DISTINCT Member.ID 
FROM Member
LEFT OUTER JOIN Experience ON Experience.MemberID = Member.ID
LEFT OUTER JOIN Qualification ON Qualification.MemberID = Member.ID
LEFT OUTER JOIN Language On Language.MemberID = Member.ID
LEFT OUTER JOIN Member_Interests ON Member_Interests.MemberID = Member.ID
LEFT OUTER JOIN Group_Members ON Group_Members.MemberID = Member.ID
WHERE 1=1 AND Gender = 'male' AND Language.Name IN ( 'Swedish' ) AND Group_Members.GroupID = 4 

但是,如果我排除直接在成员表上搜索的其他 where 条件,则 language.Name 将被忽略。IE。

SELECT DISTINCT Member.ID 
FROM Member
LEFT OUTER JOIN Experience ON Experience.MemberID = Member.ID
LEFT OUTER JOIN Qualification ON Qualification.MemberID = Member.ID
LEFT OUTER JOIN Language On Language.MemberID = Member.ID
LEFT OUTER JOIN Member_Interests ON Member_Interests.MemberID = Member.ID
LEFT OUTER JOIN Group_Members ON Group_Members.MemberID = Member.ID
WHERE 1=1 AND Language.Name IN ( 'Swedish' ) AND Group_Members.GroupID = 4

上面的查询返回组 4 中的所有成员。瑞典语部分被简单地忽略,只要语言表中有一个名为瑞典语的语言,则返回所有用户。如果语言不在表中,则不返回任何用户。

我希望第二个查询只返回第 4 组中讲瑞典语的所有成员。有谁知道为什么这没有发生?

4

4 回答 4

0

将左联接表上的过滤器移至联接。

例如:

LEFT OUTER JOIN Language On Language.MemberID = Member.ID 
     and LanguageID in ('Swedish')
LEFT OUTER JOIN Group_Members ON Group_Members.MemberID = Member.ID 
     AND Group_Members.GroupID = 4 
...
WHERE 1=1 AND Gender = 'male' 

等等

于 2012-08-17T14:46:46.583 回答
0

您是否尝试过内部连接?

要不就

SELECT DISTINCT Member.ID 
FROM Member, Language
LEFT OUTER JOIN Experience ON Experience.MemberID = Member.ID
LEFT OUTER JOIN Qualification ON Qualification.MemberID = Member.ID
LEFT OUTER JOIN Member_Interests ON Member_Interests.MemberID = Member.ID
LEFT OUTER JOIN Group_Members ON Group_Members.MemberID = Member.ID
WHERE 1=1 AND Gender = 'male' AND Language.MemberID = Member.ID AND Language.Name IN ( 'Swedish' ) AND Group_Members.GroupID = 4 
于 2012-08-17T14:58:43.693 回答
0

澄清:您最终想要什么,因为 LEFT JOIN 意味着无论左侧表和 WHERE 条件如何,但即使右侧表没有任何内容,也要抓取。将右侧表添加到 where 时,它​​实际上是作为内部连接创建的。您是否只想要明确的男性、瑞典语和第 4 组?如果是这样,我会使用普通连接

SELECT DISTINCT 
      M.ID 
   FROM Member M
      LEFT OUTER JOIN Experience E
         ON M.ID = E.MemberID 
      LEFT OUTER JOIN Qualification Q
         ON M.ID = Q.MemberID 
      LEFT OUTER JOIN Language L 
         ON M.ID = L.MemberID 
      LEFT OUTER JOIN Member_Interests MI
         ON M.ID = MI.MemberID 
      LEFT OUTER JOIN Group_Members GM
         ON M.ID = GM.MemberID 
   WHERE 
          1=1 
      AND M.Gender = 'male' 
      AND L.Name IN ( 'Swedish' ) 
      AND GM.GroupID = 4 

实际上从 LEFT JOIN 变为 INNER JOIN 只获取性别为男性、语言为瑞典语且组为 4 的记录。为了帮助更好地理解和交换查询,我展示了以下内容:

SELECT DISTINCT 
      M.ID 
   FROM Member M
      JOIN Language L 
         ON M.ID = L.MemberID 
         AND L.Name IN ('Swedish')
      JOIN Group_Members GM
         ON M.ID = GM.MemberID 
        AND GM.GroupID = 4
   WHERE 
          1=1 
      AND M.Gender = 'male' 

我删除了其他 LEFT JOIN 条目,因为它们与返回的不同成员“​​ID”值的最终请求无关。如果不使用为什么要加入。

于 2012-08-17T15:31:00.003 回答
0

如果您之前的查询给出了正确的结果。那么这个必须为你工作,你需要

SELECT DISTINCT Member.ID 
FROM Member
LEFT OUTER JOIN Experience ON Experience.MemberID = Member.ID
LEFT OUTER JOIN Qualification ON Qualification.MemberID = Member.ID
LEFT OUTER JOIN Language On Language.MemberID = Member.ID
LEFT OUTER JOIN Member_Interests ON Member_Interests.MemberID = Member.ID
LEFT OUTER JOIN Group_Members ON Group_Members.MemberID = Member.ID
WHERE 1=1 AND Gender IN ('male','female') AND Language.Name IN ( 'Swedish' ) 
AND Group_Members.GroupID = 4;

试试看,有什么问题。请发表评论,否则请享受它:)

于 2012-08-17T15:53:29.283 回答