左外连接sql中忽略的子句

时间:2012-08-17 14:35:23

标签: mysql sql

我在使用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 

但是,如果我排除另一个直接在成员表上搜索的条件,那么简单地忽略language.Name。即

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中的所有成员。瑞典语部分被忽略,只要Langauges表中有一个名为Swedish的语言,就会返回所有用户。如果表中没有该语言,则不会返回任何用户。

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

4 个答案:

答案 0 :(得分: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' 

答案 1 :(得分: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 

答案 2 :(得分: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”值的最终请求无关。为什么加入,如果不使用。

答案 3 :(得分: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;

尝试一下,如果有任何问题。评论请,否则享受它:)