2011-05-05 57 views
0

我是MYSQL的新手,但我试图从我已有的MYSQL select中获得计数。
非常多,我想计算项目的数量,然后按它们的位置分组它们.locationID然后显示locations.Name。从MYSQL查询获得计数

这是我有(有点乱)。这将获得5000MB以上的驱动器并显示它们。

Select DISTINCT 
    'C' 
    , computers.computerid 
    , computers.Name as ComputerName 
    , Convert(CONCAT(clients.name,' ',locations.name) Using utf8) As Location 
    , drives.`Size` as TestValue,0 
FROM ((drives 
LEFT JOIN Computers ON Computers.ComputerID=drives.ComputerID) 
LEFT JOIN Locations ON Locations.LocationID=Computers.Locationid) 
LEFT JOIN Clients ON Clients.ClientID=Computers.clientid 
JOIN AgentComputerData on Computers.ComputerID=AgentComputerData.ComputerID 
WHERE drives.`Size` > 5000 AND (1) 
AND Computers.ComputerID NOT IN 
    (Select ComputerID from AgentIgnore Where AgentID=0); 

预先感谢任何帮助,您可以给我。

回答

1

试试这个。如果你在任何一个表中都没有关系,那么我就会取而代之,因为结果没有意义。另外,我建议在脚本端进行任何字符集转换(如果它是一个选项),并且如果您只需要一种编码,则按照以前的方式对表进行编码。

SELECT COUNT(*) AS NumDrives 
    , CONCAT(clients.name,' ',locations.name) As Location 
    FROM drives 
INNER 
    JOIN Computers 
    ON Computers.ComputerID=drives.ComputerID 
    AND drives.`Size` > 5000 
INNER 
    JOIN AgentComputerData on Computers.ComputerID=AgentComputerData.ComputerID  
INNER 
    JOIN Locations 
    ON Locations.LocationID=Computers.Locationid 
INNER 
    JOIN Clients 
    ON Clients.ClientID=Computers.clientid 
WHERE Computers.ComputerID 
    NOT 
    IN (Select ComputerID from AgentIgnore Where AgentID=0) 
GROUP 
    BY CONCAT(clients.name,' ',locations.name) 
+0

非常好,这是方式更清洁,工作得很好,谢谢。 – Jeff 2011-05-06 12:49:47

0
Select 
    count(*) as ItemCount 
    ,Locations.Name 
FROM drives 
LEFT JOIN Computers ON Computers.ComputerID = drives.ComputerID 
LEFT JOIN Locations ON Locations.LocationID = Computers.Locationid 
LEFT JOIN Clients ON Clients.ClientID = Computers.clientid 
JOIN AgentComputerData on Computers.ComputerID = AgentComputerData.ComputerID 
WHERE drives.`Size` > 5000 
AND Computers.ComputerID NOT IN 
    (Select ComputerID from AgentIgnore Where AgentID=0); 
GROUP BY Locations.LocationID 

我试着坚持你的描述接近我所能。希望这是你所需要的。