0

我正在使用 Mysql,但我很难从 SELECT 查询中获取结果。我有 2 张桌子。第一个表硬件,第二个表softwares_name_cache:

desc hardware;
+----------------+---------------------+------+-----+---------+----------------+
| Field          | Type                | Null | Key | Default | Extra          |
+----------------+---------------------+------+-----+---------+----------------+
| ID             | int(11)             | NO   | PRI | NULL    | auto_increment |
| DEVICEID       | varchar(255)        | NO   | PRI | NULL    |                |
| NAME           | varchar(255)        | YES  | MUL | NULL    |                |
| WORKGROUP      | varchar(255)        | YES  | MUL | NULL    |                |
| USERDOMAIN     | varchar(255)        | YES  |     | NULL    |                |
| OSNAME         | varchar(255)        | YES  | MUL | NULL    |                |
| OSVERSION      | varchar(255)        | YES  |     | NULL    |                |
| OSCOMMENTS     | varchar(255)        | YES  |     | NULL    |                |
| PROCESSORT     | varchar(255)        | YES  |     | NULL    |                |
| PROCESSORS     | int(11)             | YES  |     | 0       |                |
| PROCESSORN     | smallint(6)         | YES  |     | NULL    |                |
| MEMORY         | int(11)             | YES  | MUL | NULL    |                |
| SWAP           | int(11)             | YES  |     | NULL    |                |
+----------------+---------------------+------+-----+---------+----------------+

desc softwares_name_cache;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| ID    | int(11)      | NO   | PRI | NULL    | auto_increment |
| NAME  | varchar(255) | YES  | UNI | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

我正在尝试获取每个 SQL 服务器的处理器数量,因此我需要从表硬件(sum(hardware.PROCESSORN)中加入条件,并从表 softwares_name_cache 中加入条件。 (OSNAME LIKE '%Server%')(softwares_name_cache where name like '%Microsoft SQL Server 20%')

4

2 回答 2

0
SELECT sum(hardware.PROCESSORN) 
FROM hardware 
inner join softwares_name_cache on hardware.ID= softwares_name_cache.ID 
WHERE (OSNAME LIKE '%Server%') 
AND softwares_name_cache.NAME IN 
    (
     select softwares_name_cache.NAME
     from softwares_name_cache 
     where softwares_name_cache.NAME like '%Microsoft SQL Server 20%'
    );
于 2017-08-23T07:35:22.547 回答
0

由于“名称”字段对于硬件和软件名称缓存表都是通用的。在您的 where 条件中,您正在使用 Name filed 进行过滤。因此它会引发错误。试试这样:

SELECT sum(hardware.PROCESSORN) 
FROM hardware 
inner join softwares_name_cache on hardware.id = softwares_name_cache.ID 
WHERE (OSNAME LIKE '%Server%') AND softwares_name_cache.NAME IN  IN (select NAME from 
      softwares_name_cache where name like '%Microsoft SQL Server 20%');
于 2017-08-23T07:33:12.857 回答