2012-09-18 67 views
0

我想在postgresql中選擇不同的查詢中放置where子句,但沒有運氣。DISTINCT WHERE在PostgreSQL中查詢

我要做到以下幾點:

select distinct on (state_or_county) state_or_county 
from locations 
where country = "USA" 

我已經做了在MySQL這個很多次,不明白爲什麼這是行不通的。

有人可以糾正查詢嗎?或者解釋爲什麼它不起作用?

回答

2

在PostgreSQL字符串文本必須被包裹在單引號:

select distinct on (state_or_county) state_or_county 
from locations 
where country = 'USA' 

雙引號用於標識符,如果需要的話:

select distinct on ("state_or_county") "state_or_county" 
from "locations" 
where "country" = 'USA' 
+0

注意這是SQL規範怎麼說的做的事情。再一次地,MySQL對SQL規範的使命傾向意味着它做的事情與你預期的有點不同,並且導致用戶在切換到遵循規則的另一個數據庫時學習壞習慣。 –