2011-02-02 55 views
2

算哪里的正则表达式组你可以看到什么即时尝试做的:选择并计数

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

带可以是:

avenged sevenfold 
3 days grace 
led zeppelin 
98 mute 
back street boys 
beastie boys 

我需要这个来选择其乐队首字符不是一个alpha,并计算每个band有多少行。

不幸的是,我目前的查询似乎只是将它们全部组合在一起,与REGEXP匹配。

回答

0

是否在组帮助后做选择?

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

此外,您似乎选择不在组子句中的列。尝试:

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

您无法选择不在group by子句既不是一组功能列(COUNT,MAX ...)

的地方是OK的,因为你并不需要将不需要的行分组,并且条件不会超出组值(组函数的结果)。

ASC是默认的排序方式,所以你不需要指定它。

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