MySQL左连接而不是等于

Chr*_* So 2 php mysql join left-join

我搞砸了MySQL Query ...我想在另一个表中获取一些不存在某些值的表记录,但我不想使用NOT IN操作.然后,我尝试使用LEFT JOIN来显示不匹配的数据.但是,它显示空结果:

tbl_comment

comment_id | comment_message
----------------------------
1_1        | some text1...
1_2        | some text2...
1_3        | some text3...
2_1        | some text4...
2_2        | some text5...
2_3        | some text6...
Run Code Online (Sandbox Code Playgroud)

tbl_analysis

analysis_id | analysis_message_id
----------------------------
1.0.1       | 1_1
1.0.1       | 1_2
1.0.1       | 2_1
1.0.2       | 1_3
1.0.3       | 2_2
Run Code Online (Sandbox Code Playgroud)

我的错误查询(空结果):

SELECT comments.* ,
       analysis.*
FROM tbl_comment as comments

LEFT JOIN tbl_analysis as analysis

ON comments.comment_id = analysis.analysis_message_id

WHERE analysis.analysis_id != '1.0.1'
Run Code Online (Sandbox Code Playgroud)

建议的结果:(找到所有的评论认为,analysis_id不等于1.0.1或不存在tbl_analysis)

  comment_id  | comment_message | analysis_id | analysis_message_id
    ----------------------------------------------------------------
    1_3        | some text3...   | 1.0.2       | 1_3
    2_2        | some text5...   | 1.0.3       | 2_2
    2_3        | some text6...   | NULL        | NULL
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助......

Nau*_*hal 5

试试这个

SELECT comments.* , analysis.*
FROM tbl_comment as comments
LEFT JOIN tbl_analysis as analysis
ON comments.comment_id = analysis.analysis_message_id
WHERE analysis.analysis_id IS NULL
//IS NULL OR IS NOT NULL according to you requirement.
Run Code Online (Sandbox Code Playgroud)


Tom*_*Mac 5

您正在执行表格,但您已LEFT JOINcomments表格中的一列上包含谓词,commentsWHERE analysis.analysis_id != '1.0.1'.这将否定你的LEFT JOIN.试试这个

SELECT comments.* ,
       analysis.*
FROM tbl_comment as comments
LEFT JOIN tbl_analysis as analysis
ON comments.comment_id = analysis.analysis_message_id AND analysis.analysis_id != '1.0.1'
;
Run Code Online (Sandbox Code Playgroud)