2

我有以下sql:

SELECT `id` FROM `exchange` WHERE `photo` = 1 ORDER BY `id` DESC LIMIT 18

我怎样才能得到不同的location值?

例如这样做但它返回一个错误:

SELECT `id`, distinct(`location`) FROM `exchange` WHERE `photo` = 1 ORDER BY `id` DESC LIMIT 18

示例表:

id    location

1        7
2        8
3        4
4        4
5        2
6        32

示例结果(当 LIMIT 5 时):

6,5,4,2,1

4

1 回答 1

1

DISTINCT适用于整行,而不适用于每列的值。删除该列ID,您将拥有唯一的位置。

SELECT distinct `location`
FROM `exchange` 
WHERE `photo` = 1 
ORDER BY `id` DESC 
LIMIT 18

更新 1

您可以使用MAX()之类的AGGREGATE FUNCTION并按它们的. 再试一次,ID

SELECT location, MAX(ID) ID
FROM exchange
GROUP BY location
于 2012-09-20T00:54:40.903 回答