简体   繁体   English

查询不能在左联接上工作?

[英]Query not working on left join?

I have following query which gives me result with single marstat. 我有以下查询,这给了我single marstat结果。

SELECT
  a_event . *,
  members.*  
FROM a_event
  INNER JOIN members
    ON (members.mem_id = a_event.a_uid)
  LEFT JOIN `profiles`
    ON (profiles.mem_id = members.mem_id
        AND profiles.marstat =  'Single')
WHERE a_event.a_event_id = '5496'
    AND members.gender = 'm'
    AND a_event.a_fb_event_id = '0'

But the actual situation is that a_event member must match with member table (INNER JOIN is compulsory) then this result will match with profiles table 's mem_id having marital status as single . 但是实际情况是a_event成员必须与成员表匹配(INNER JOIN是强制性的),然后此结果将与具有婚姻状态为single的个人资料表的mem_id匹配。 But the main problem is profile table may contain result with profiles.mem_id or no entry so that user from a_event.mem_id if not present in profiles.mem_id then that user will also be considered as single 但是主要问题是配置文件表可能包含带有profiles.mem_id的结果或没有条目,因此如果a_event.mem_id中的用户不存在于profiles.mem_id中,那么该用户也将被视为single

Please help thanks 请帮忙谢谢

If I understand your question correctly you want a row to be returned when: 如果我正确理解了您的问题,则希望在以下情况下返回行:

a) the user has a profile and is single b) the user has no profile a)用户有个人资料并且是单身b)用户没有个人资料

In that case the query should be: 在这种情况下,查询应为:

SELECT
  a_event . *,
  members.*  
FROM a_event
  INNER JOIN members
    ON (members.mem_id = a_event.a_uid)
  LEFT JOIN `profiles`
    ON (profiles.mem_id = members.mem_id)
WHERE a_event.a_event_id = '5496'
    AND members.gender = 'm'
    AND a_event.a_fb_event_id = '0'
    AND (profiles.marstat =  'Single' OR profiles.marstat IS NULL)

without a fiddle it's hard to test, but try this: updated answer with fiddle 没有小提琴就很难测试,但是尝试一下:用小提琴更新答案

    SELECT
      a_event . *,
      members.*  
    FROM a_event
      INNER JOIN members
        ON (members.mem_id = a_event.a_uid)
      LEFT JOIN `profiles`
        ON (profiles.mem_id = members.mem_id)
    WHERE a_event.a_event_id = '5496'
        AND members.gender = 'm'
        AND a_event.a_fb_event_id = '0'
        AND (profiles.marstat =  'Single' or is null)

http://sqlfiddle.com/#!2/99b774/3 http://sqlfiddle.com/#!2/99b774/3

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM