SQL Server:来自两个表的查询关联 [英] SQL Server : query correlation from two tables
问题描述
我有两个表,试图将来自以下表的数据关联起来:
I have two tables that I am trying to correlate data from:
Tasklist
:
Hostname PID Process Module
-----------------------------------------------
Host1 1 sample.exe sample.dll
Host2 4 sample2.exe sample2.dll
Host5 11 sample5.exe sample.dll
和
Netstat
:
Hostname PID Address .... (many other columns I don't care about)
------------------------------
Host9 8 10.10.10.10
Host2 67 78.66.44.22
Host8 78 54.22.11.33
Host1 1 77.33.22.11
我想做的是从任务列表表中返回所有内容,以便在Netstat表中也找到任务列表表中的主机名和PID.因此,在这种情况下,由于Hoststat和PID 1存在于Netstat中,因此它将返回Tasklist表的第一行.
What I want to do is return everything from the tasklist table for lines in which the Hostname and PID from the Tasklist table are also found in the Netstat table. So in this case, it would return the first row from the Tasklist table since Host1 and PID 1 exist in Netstat.
我对SQL还是很陌生,真的不知道该怎么做,我们将不胜感激!
I'm fairly new to SQL and don't really know how to do this, any help would be appreciated!
推荐答案
您应该在两个表上使用join来从两个表中获取匹配的值,但是当您需要来自tasklist
的值时.你可以做这样的事情,
You should use join on both tables to get matching values from both tables but as you need values from tasklist
. You can do something like this,
Select tasklist.*,netstat.address
From tasklist join netstat
On tasklist.pid = netstat.pic and tasklist.hostname = netstat.hostname
这篇关于SQL Server:来自两个表的查询关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!