如何加入两个查询?

时间:2016-08-10 08:08:29

标签: sql

我有2个单独的查询,想要join他们并且不知道如何。如果我运行以下操作,我会收到错误:

  

使用的SELECT语句具有不同的列数。

我如何成功join

第一次查询

SELECT

hardware_info.id as ComputerID,
software_info.OSPlatform as OperatingSystemName,
software_info.OSVersion as OSVersion,
agent_info.AgentName as DeviceName,
hardware_info.last_modified as LastUpdate,
software_info.AD_MachineOUPath as NTDomainName,
software_info.AD_UserOUPath as AD_Site,
hardware_info.ComputerManufacturer as Manufacturer,
hardware_info.MachineModel as Model,
hardware_info.CPUCount as NoOfCPUs,
software_info.OSPlatform as OperatingSystemName,
hardware_info.MachineSerialno as SerialNumber,
hardware_info.CPUIDString as CPUDescription,
hardware_info.CPUSpeed as MaxClockSpeed,
hardware_info.PrimaryMACAddress as PhysicalAddress,
hardware_info.PhysicalMemorySize as Memory    

FROM
agent_info,
hardware_info,
software_info,
machine_tracking_info,
installed_software_titles     
WHERE
agent_info.heartbeat_record_id = hardware_info.agent_info_record_id limit 10

样本结果

ComputerID 1    
OperatingSystemName 116
OSVersion 1584  
DeviceName USALVWAM1    
LastUpdate 2016-07-08 04:27:01  
NTDomainName Servers\from New Computers
AD_Site Global-Users\US\CS  
Manufacturer    VMware, Inc.    
Model   GenericPC
NoOfCPUs    1   
OperatingSystemName SerialNumber VMware-42 04 90 30 1d a2 bc dc-69 a6 eb    47  68 76 b1 3b    
CPUDescription  Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz
MaxClockSpeed   2500000000  
PhysicalAddress 00:50:56:84:40:50
Memory 4294967296

**second query**
SELECT 
enum_value as osplatform 
FROM
software_info
INNER JOIN
    enum_osplatform ON software_info.OSPlatform = enum_osplatform.enum_key

**SAMPLE RESULT**

osplatform windows 2012 r2

2 个答案:

答案 0 :(得分:2)

两个查询中的数字字段必须相等。

试试这个

 SELECT 
    hardware_info.id as ComputerID,
    agent_info.AgentName as DeviceName,
    hardware_info.last_modified as LastUpdate,
    software_info.AD_MachineOUPath as NTDomainName,
    software_info.AD_UserOUPath as AD_Site,
    hardware_info.ComputerManufacturer as Manufacturer,
    hardware_info.MachineModel as Model,
    hardware_info.CPUCount as NoOfCPUs,         
    hardware_info.MachineSerialno as SerialNumber,
    hardware_info.CPUIDString as CPUDescription,
    hardware_info.CPUSpeed as MaxClockSpeed,
    hardware_info.PrimaryMACAddress as PhysicalAddress,
    hardware_info.PhysicalMemorySize as Memory, 
    software_info.OSVersion ,
    software_info.OSPlatform as OperatingSystemName,
    null      
FROM
    agent_info,
    hardware_info,
    software_info,
    machine_tracking_info,
    installed_software_titles     
WHERE
    agent_info.heartbeat_record_id = hardware_info.agent_info_record_id

union

SELECT 
    null as ComputerID,
    null as DeviceName,
    null as LastUpdate,
    null as NTDomainName,
    null as AD_Site,
    null as Manufacturer,
    null as Model,
    null as NoOfCPUs,           
    null as SerialNumber,
    null as CPUDescription,
    null as MaxClockSpeed,
    null as PhysicalAddress,
    null as Memory,
    null as OSVersion,
     osplatform ,enum_value
FROM
    software_info
INNER JOIN
    enum_osplatform ON software_info.OSPlatform = enum_osplatform.enum_key

答案 1 :(得分:0)

如上所述,您可以UNION并在第二部分使用NULL

我接触的方式不同了。只需在提供信息时填写XXX。

SELECT
h.id AS ComputerID,
a.AgentName AS DeviceName,
h.last_modified AS LastUpdate,
s.AD_MachineOUPath AS NTDomainName,
s.AD_UserOUPath AS AD_Site,
h.ComputerManufacturer AS Manufacturer,
h.MachineModel AS Model,
h.CPUCount AS NoOfCPUs,
s.OSPlatform AS OperatingSystemName,
h.MachineSerialno AS SerialNumber,
h.CPUIDString AS CPUDescription,
h.CPUSpeed AS MaxClockSpeed,
h.PrimaryMACAddress AS PhysicalAddress,
h.PhysicalMemorySize AS Memory, 
s.OSVersion,
enum_value as OSPlatform
FROM agent_info a
INNER JOIN hardware_info h ON a.heartbeat_record_id = h.agent_info_record_id 
INNER JOIN software_info s ON a.heartbeat_record_id = s.agent_info_record_id
INNER JOIN enum_osplatform e ON s.OSPlatform = e.enum_key