在mysql查询的两列中匹配值

时间:2013-12-18 03:11:37

标签: mysql sql multiple-columns

尝试匹配同一个表中的两列。希望看到一个设备的输出提供另一个设备的输入。这是一个数据样本。我知道格式化很糟糕,但我确实尝试过!有四列。

name    output_ip   input_ip    service  
s101    69.255.255.12   192.168.1.2 apple  
s101    69.255.255.12   192.168.1.3 orange  
s102    69.252.102.102  192.168.1.4 grape  
s102    69.252.102.102  192.168.1.5 kiwi  
s103    10.89.96.102    192.168.1.6 strawberry  
s103    10.89.96.102    192.168.1.7 banana  
s104    10.252.36.21    192.168.1.8 pear  
s104    10.252.36.21    192.168.1.9 plum  
r101    192.168.0.23    69.255.255.12   
r101    192.168.0.23    69.255.255.12   
r102    192.168.0.24    69.252.102.102  
r102    192.168.0.24    69.252.102.102  
r103    192.168.0.25    10.89.96.102    
r103    192.168.0.25    10.89.96.102    
r104    192.168.0.26    10.252.36.21    
r104    192.168.0.26    69.255.255.12

想要像这样输出

s101 r101 apple 69.255.255.12   
s101 r101 orange 69.255.255.12   
s102 r102 grape 69.252.102.102   
S102 r102 kiwi 69.252.102.102   
S103 r103 strawberry 10.89.96.102   
S103 r103 banana 10.89.96.102   
S104 r104 pear 10.252.36.21   
S101 r104  69.255.255.12  

1 个答案:

答案 0 :(得分:0)

我不是100%确定匹配数据的标准是什么。

我最好的猜测是你需要自己加入表,假设表名为data

SELECT
    s.name,
    r.name,
    s.service,
    s.output_ip
FROM
    data AS s
    JOIN data AS r ON s.output_ip = r.input_ip
GROUP BY
    s.name, r.name, s.output_ip. s.service