简体   繁体   中英

MySQL - Use Distinct in Where

I have am working with 2 columns, one that should be unique but has duplicates that i want to remove and one column that i need a count of the duplicates. I have the second half of the equation figured out finally, but i still need to figure out how to remove the duplicates from the first column.

SELECT map.newvalue as 'Model of Phone', Count(*) as 'Number of Phones'
FROM items i, hosts h, history_uint huint, mappings map
WHERE h.hostid=i.hostid AND h.name='$Hosts' AND i.itemid=huint.itemid AND i.valuemapid=map.valuemapid AND huint.value=map.value AND i.name LIKE '%Model of the Phone'
GROUP BY map.newvalue
ORDER BY 'Item Name' DESC
LIMIT 100;

The table.column that i am getting duplicate values from is i.name . Sorry if my query looks awful, I am just an IT guy pretending to know SQL. Thank you for the assist!

Incase anyone else looks at this later in life, i am working on collecting phone models from Call Manager via SNMP and Zabbix and displaying it in Grafana.

You can not use distinct in where clause but you can use group by for multiple column Try with this

SELECT map.newvalue as 'Model of Phone', Count(*) as 'Number of Phones'
FROM items i, hosts h, history_uint huint, mappings map
WHERE h.hostid=i.hostid AND h.name='$Hosts' AND i.itemid=huint.itemid AND i.valuemapid=map.valuemapid AND huint.value=map.value AND i.name LIKE '%Model of the Phone'
GROUP BY map.newvalue, i.name
ORDER BY 'Item Name' DESC
LIMIT 100;

It will change your result a bit because of adding extra filed in group by but it will give you unique name and newvalue result.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM