I want a query that will give me all results that have a distinct key, and if there are duplicate entries(ignoring the fact that _id would be different), it will give back only the first one.....the _id isn't what I want to be distinct, as they [the ids] are already unique. ... . Ideally it will order by 'available' in descending order so that if there are two columns with the same data(aside from _id and available), it will return the row with '1' for the available column
select name, image, address, max(availability) as avail
from T
group by name, image, address
Затем вы можете присоединить набор, возвращенный запросом выше, в виде встроенного представления, к вашей таблице:
select * from T
inner join
(
select name, image, address, max(availability) avail
from T
group by name, image, address
) as foo
on T.name = foo.name and T.image = foo.image and T.address = foo.address and T.availability = foo.avail
Это поможет получитьсоставной индекс так: (имя, изображение, адрес).
Предостережение: если имеется более одной строки, в которой конкретная триада {имя, изображение, адрес} имеет доступность = 1, запрос возвратит несколько строкдля триады:
2 | John | http://img.com/222 | 1 Main St | 1
6 | John | http://img.com/222 | 1 Main St | 1
PS Звучит так, как будто вы хотели, чтобы триада (имя, изображение, адрес) была создана в вашей таблице альтернативным уникальным ключом.