选择字段正则表达组所在的字段

时间:2011-02-02 16:47:42

标签: mysql

我正在尝试编写一个查询,根据字段中第一个字符不是alpha的事实来选择行。

示例,假设我在表格中有以下数据:

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

这根本不起作用。

2 个答案:

答案 0 :(得分:0)

我已经在我的数据库中尝试了你的查询并且它运行得很好,所以问题不在于REGEXP而是其他问题。标题和文章是我的数据库中的列。有趣的是AS 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 |
+--------------+-------+

答案 1 :(得分:0)

尝试在查询的第一部分中封装字段名称Titlecount。虽然它似乎适用于stephanie gratzer,但您的配置可能是一个问题,即您的字段名称是SQL关键字。封装它/可能/解决这个(潜在的)问题。

相关问题