从MYSQL查询中获取计数
我是MYSQL的新手,但我正在尝试从我已有的MYSQL选择中获取计数。从MYSQL查询中获取计数,mysql,Mysql,我是MYSQL的新手,但我正在尝试从我已有的MYSQL选择中获取计数。 基本上,我想数一数项目的数量,然后根据它们的位置对它们进行分组。locationID是,然后显示locations.Name 这是我的(有点乱)。这将获取5000MB以上的驱动器并显示它们 Select DISTINCT 'C' , computers.computerid , computers.Name as ComputerName , Convert(CONCAT(clients.name,' ',
基本上,我想数一数项目的数量,然后根据它们的位置对它们进行分组。locationID是,然后显示locations.Name 这是我的(有点乱)。这将获取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);
提前感谢您能给我的任何帮助。试试这个。我替换了lefts win inners,因为如果在任何表中缺少关系,则结果毫无意义。此外,我还建议在脚本端执行任何字符集转换(如果这是一个选项),并且如果您只需要一种编码,请按照这种方式对表进行编码
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)
我尽可能地坚持你的描述。希望这是你需要的
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