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
This commit is contained in:
Zachary Wasserman 2017-02-17 11:19:27 -08:00 committed by GitHub
parent 8b4b43fb82
commit 3bab6bae18
2 changed files with 7 additions and 2 deletions

View file

@ -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 {

View file

@ -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)