我正在尝试在查询中使用DISTINCT,但是我无法正确获得所需的语法。我做了几个不同的堆栈溢出文章,我想我终于了解了如何包括获得帮助所需的所有信息。
我在DB Fiddle上构建了一个共享空间,其中所有信息已在此处加载: https://www.db-fiddle.com/f/b2o3Wy31hLmwSZLAfpuaVr/0#&togetherjs=IrGzVdtmMh
CREATE TABLE hosts (
hostid INT NOT NULL,
name VARCHAR(30) NOT NULL,
PRIMARY KEY (hostid),
UNIQUE (name)
);
INSERT INTO hosts
(hostid, name)
VALUES
(10761,"CUCM1"),
(10762,"CUCM2"),
(10763,"CUCM3");
CREATE TABLE items (
hostid INT NOT NULL,
itemid INT NOT NULL,
name VARCHAR(60) NOT NULL,
valuemapid INT NOT NULL
);
INSERT INTO items
(hostid,itemid,name,valuemapid)
VALUES
(10761,304827,"Phone 33 44 55 66 77 88:Model of the Phone",68),
--phone1
(10761,304827,"Phone 33 44 55 66 77 88:Model of the Phone",68),
--phone1
(10761,304827,"Phone 33 44 55 66 77 88:Model of the Phone",68),
--phone1
(10761,304828,"Phone 33 44 55 66 88 88:Model of the Phone",68),
--phone2
(10761,304828,"Phone 33 44 55 66 88 88:Model of the Phone",68),
--phone2
(10761,304828,"Phone 33 44 55 66 88 88:Model of the Phone",68),
--phone2
(10761,304829,"Phone 33 44 55 77 77 88:Model of the Phone",68),
--phone3
(10761,304829,"Phone 33 44 55 77 77 88:Model of the Phone",68),
--phone3
(10761,304820,"Phone 33 44 44 66 77 88:Model of the Phone",72),
--phone4
(10761,304820,"Phone 33 44 44 66 77 88:Model of the Phone",72);
--phone4
CREATE TABLE history_uint (
itemid INT NOT NULL,
value INT NOT NULL
);
INSERT INTO history_uint
(itemid,value)
VALUES
(304827,109),
(304828,109),
(304829,109),
(304820,110);
CREATE TABLE mappings (
valuemapid INT NOT NULL,
value INT NOT NULL,
newvalue VARCHAR (30) NOT NULL,
PRIMARY KEY (valuemapid)
);
INSERT INTO mappings
(valuemapid,value,newvalue)
VALUES
(68,109,"Cisco 7841"),
(72,110,"Cisco 7940");
这是我能够拼凑的查询
SELECT map.newvalue as 'Model of Phone', Count(*) as 'Number of Phones'
FROM hosts h
LEFT JOIN items i
ON h.hostid=i.hostid
LEFT JOIN history_uint huint
ON i.itemid=huint.itemid
LEFT JOIN mappings map
ON i.valuemapid=map.valuemapid
WHERE huint.value=map.value AND i.name LIKE '%Model of the Phone'
GROUP BY map.newvalue;
现在查询返回
Model of Phone Number of Phones
Cisco 7841 8
Cisco 7940 2
但是我希望在item表colomn itemid上使用DISTINCT,所以我们只能返回
Model of Phone Number of Phones
Cisco 7841 3
Cisco 7940 1
谢谢您的帮助!
答案 0 :(得分:1)
如果您DISTINCT
COUNT
会怎样?
SELECT map.newvalue as 'Model of Phone'
, Count(DISTINCT i.name) as 'Number of Phones'
FROM hosts h
JOIN items i ON h.hostid = i.hostid
JOIN history_uint huint ON i.itemid = huint.itemid
JOIN mappings map ON i.valuemapid = map.valuemapid AND huint.value=map.value
WHERE i.name LIKE '%Model of the Phone'
GROUP BY map.newvalue;
答案 1 :(得分:0)
由于您特别希望从items表中获得不同的记录来为您提供电话类型,因此您的查询应该类似于以下内容:
SELECT map.newvalue as 'Model of Phone', Count(distinct i.itemid) as
'Number of Phones'
FROM hosts h
LEFT JOIN items i
ON h.hostid=i.hostid
LEFT JOIN history_uint huint
ON i.itemid=huint.itemid
LEFT JOIN mappings map
ON i.valuemapid=map.valuemapid
WHERE huint.value=map.value AND i.name LIKE '%Model of the Phone'
GROUP BY map.newvalue;
当执行count(*)时,之所以返回8行作为计数,是因为当您将itemid(来自history_uint)添加到选择字段时,每一行都呈现出不同的效果。因此,总的来说,这些字段不是唯一的,因此返回了全部8行。