2012-08-27 21 views

回答

10

使用內置SUBSTRING功能和DISTINCT返回不同的結果(沒有重複

SELECT DISTINCT LOWER(SUBSTRING(names, 1,1)) x 
FROM t 
ORDER BY x; 

MySQL: SUBSTRING

SQLFiddle Demo

+0

我同時得到小寫字母和大寫字母,就像alphin Alan輸出的那樣A – dude

+0

所以你的意思是小寫'a'與大寫'A'不同? –

+0

我的意思是它應該只顯示名稱以a或A開頭作爲特定 – dude

5

你可以用做到這一點

SELECT DISTINCT LOWER(LEFT(names,1)) tt 
FROM myTable 
ORDER BY tt; 

,或者您也可以做同樣的SUBSTRING()

SELECT DISTINCT LOWER(SUBSTRING(names, 1,1)) tt 
FROM myTable 
ORDER BY tt; 

See this Fiddle

+0

http://sqlfiddle.com/#!1/f0e30/2我需要postgresql – dude

+0

@dude你是用PostgreSQL還是MySQL來試試這個? 您的演示在PostgreSQL中。 – hims056

+0

yup @ hims056 !!! m試試postgre – dude

相關問題