2011-02-02 69 views
1

我想寫一個查詢,將根據該字段中的第一個字符不是字母的事實來選擇行。選擇字段,其中字段正則表達式組由

例子,可以說我在表中的下列數據:

id | band | song 
1 | 3 days grace | song name 
2 | avenged sevenfold | song name 
3 | as i lay dying | song name 
4 | 98 mute | song name 
5 | 98 mute | another song name 

我需要這個選擇:

3 days grace (and show a count of 1) 
98 mute (and show a count of 2) 

這裏是我的代碼:

select band, count(*) as count 
from `songs` 
where `band` REGEXP '^[^[:alpha:]]' 
group by `band` 
order by `band` asc 

這根本不工作。

+1

您的語法是正確的,查詢必須工作。什麼是MySQL的版本?向我們展示您請的結果... – 2011-02-02 17:00:32

回答

0

我已經在我的數據庫中試過了你的查詢,它工作得很好,所以問題不在於REGEXP,而在於別的。標題和文章是我的數據庫中的列。有趣的是,如果COUNT沒有將計數封裝在``中,它是如何工作的。

mysql> select Title, count(*) as count 
    -> from `Articles` 
    -> where `Title` REGEXP '^[^[:alpha:]]' 
    -> group by `Title` 
    -> order by `Title` asc; 

+--------------+-------+ 
| Title  | count | 
+--------------+-------+ 
| 3 days grace |  1 | 
| 98 mute  |  2 | 
+--------------+-------+ 
+0

你是對的,它在實際的行值本身是錯誤的。我改變它使用樂隊slug字段,它的工作原理。 – scarhand 2011-02-02 20:53:44

0

嘗試在查詢的第一部分封裝字段名Titlecount。雖然它似乎適用於stephanie gratzer,但對於您的配置來說,您的字段名稱可能是SQL關鍵字,但可能會出現問題。封裝它/可能/解決這個(潛在)問題。