Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL的两个字段上的SQL左键联接_Mysql_Sql_Join_Left Join - Fatal编程技术网

MySQL的两个字段上的SQL左键联接

MySQL的两个字段上的SQL左键联接,mysql,sql,join,left-join,Mysql,Sql,Join,Left Join,我有一个视图a和一个视图B 在A中,我有很多关于某些系统的信息,比如IP和port,我想保留这些信息。在B中,我只想在A添加一个信息 两个视图之间的匹配字段是IP和Port。因此,我必须匹配在两个视图中具有相同IP和端口的主机 示例: 视图A: 视图B: 输出 注意:视图A的某些主机可能在视图B中没有IP/端口相关项 视图A的某些主机也可能与视图B中的某些主机相匹配 我认为我应该使用LEFT JOIN,以便拥有视图A的所有条目和视图B的正确关联条目,但它不起作用。 我无法使用right WHER

我有一个视图
a
和一个视图
B

A
中,我有很多关于某些系统的信息,比如
IP
port
,我想保留这些信息。在
B
中,我只想在
A
添加一个信息

两个视图之间的匹配字段是
IP
Port
。因此,我必须匹配在两个视图中具有相同IP和端口的主机

示例:

视图A: 视图B: 输出 注意:视图A的某些主机可能在视图B中没有IP/端口相关项

视图A的某些主机也可能与视图B中的某些主机相匹配

我认为我应该使用LEFT JOIN,以便拥有视图A的所有条目和视图B的正确关联条目,但它不起作用。 我无法使用right WHERE子句和JOIN解决方案调整查询

有什么想法吗?

让我们这样试试:

select a.ip, a.os, a.hostname, a.port, a.protocol,
       b.state
from a
left join b on a.ip = b.ip 
           and a.port = b.port
select 
    a.ip, 
    a.os, 
    a.hostname, 
    a.port, 
    a.protocol, 
    b.state
from a
left join b 
    on a.ip = b.ip 
        and a.port = b.port /*if you has to filter by columns from right table , then add this condition in ON clause*/
where a.somecolumn = somevalue /*if you have to filter by some column from left table, then add it to where condition*/
...
where b.somecolumn <> (=) null
所以,在
where
子句中,您只能通过以下方式从右表中按列筛选结果集:

select 
    a.ip, 
    a.os, 
    a.hostname, 
    a.port, 
    a.protocol, 
    b.state
from a
left join b 
    on a.ip = b.ip 
        and a.port = b.port /*if you has to filter by columns from right table , then add this condition in ON clause*/
where a.somecolumn = somevalue /*if you have to filter by some column from left table, then add it to where condition*/
...
where b.somecolumn <> (=) null
。。。
其中b.somecolumn(=)为空

您尝试了什么?向我们展示您的代码示例您的
WHERE
子句包含哪些内容?谢谢!我缺少的是ON子句中的double=!谢谢你的回答和WHERE子句的细节。
...
where b.somecolumn <> (=) null