From 3bab6bae185fae26459b799f004b683e10f6280e Mon Sep 17 00:00:00 2001 From: Zachary Wasserman Date: Fri, 17 Feb 2017 11:19:27 -0800 Subject: [PATCH] Use SELECT DISTINCT in favor of GROUP BY (#1251) In some MySQL configurations, using a GROUP BY that doesn't refer to every column in the SELECT will throw errors. Replace the use of GROUP BY with SELECT DISTINCT as this is also more clear as to the intentions of the query. Fixes #1249 --- server/datastore/mysql/labels.go | 3 +-- server/kolide/labels.go | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/server/datastore/mysql/labels.go b/server/datastore/mysql/labels.go index 7e32c03482..0503de3e0d 100644 --- a/server/datastore/mysql/labels.go +++ b/server/datastore/mysql/labels.go @@ -185,14 +185,13 @@ func (d *Datastore) ListUniqueHostsInLabels(labels []uint) ([]kolide.Host, error } sqlStatement := ` - SELECT h.* + SELECT DISTINCT h.* FROM label_query_executions lqe JOIN hosts h ON lqe.host_id = h.id WHERE lqe.label_id IN (?) AND lqe.matches = 1 AND NOT h.deleted - GROUP BY h.id; ` query, args, err := sqlx.In(sqlStatement, labels) if err != nil { diff --git a/server/kolide/labels.go b/server/kolide/labels.go index a581f04443..00a2727abf 100644 --- a/server/kolide/labels.go +++ b/server/kolide/labels.go @@ -29,7 +29,13 @@ type LabelStore interface { // LabelsForHost returns the labels that the given host is in. ListLabelsForHost(hid uint) ([]Label, error) + // ListHostsInLabel returns a slice of hosts in the label with the + // given ID. ListHostsInLabel(lid uint) ([]Host, error) + + // ListUniqueHostsInLabels returns a slice of all of the hosts in the + // given label IDs. A host will only appear once in the results even if + // it is in multiple of the provided labels. ListUniqueHostsInLabels(labels []uint) ([]Host, error) SearchLabels(query string, omit ...uint) ([]Label, error)