Sql查询不返回null值

时间:2016-05-22 16:33:30

标签: mysql sql

我有两张桌子:

  1. req_docs
  2. req_docs

    和pers_docs

    enter image description here

    我的SQL查询:

    SELECT *
    FROM req_docs
    LEFT OUTER JOIN pers_docs ON req_docs.doc_nid = pers_docs.doc_nid
    WHERE req_docs.pos_id ="CPT"
      AND pers_docs.pers_nid = 6
      AND pers_docs.expires <= "2009-09-01"`
    

    它只返回2行。哪个好,但我还需要将pers_docs join中不存在的匹配作为NULL值返回。请帮忙!

1 个答案:

答案 0 :(得分:2)

LEFT JOIN时,在ON子句中放置右侧表格条件以获得真正的左连接行为! (在WHERE时,您会获得定期INNER JOIN结果。)

SELECT *
FROM req_docs
LEFT OUTER JOIN pers_docs ON req_docs.doc_nid = pers_docs.doc_nid
                          AND pers_docs.pers_nid = 6
                          AND pers_docs.expires <= "2009-09-01"
WHERE req_docs.pos_id = "CPT"