sql - 在sql中按字符串的部分分组

时间:2012-02-21 17:22:00

标签: mysql sql

我正在使用sql将这样的表拉到一起:

person         flavour                                purchase
andy         likes-orange_hates-vanilla                  $5
george       likes-orange_hates-icecream                 $5
gena         likes-vanilla_hates-bourban                 $2
gena         likes-vanilla_hates-bourban                 $4

是否有可能从那里开始:

                total
likes-orange     $10
likes-vanilla    $6

更好的是

              total      av. purchase size     av. purchases per person    total people
likes-orange    $10                $5                   1                        2
likes-vanilla   $6                 $3                   2                        1

我只能访问phpmyadmin。

2 个答案:

答案 0 :(得分:10)

尝试

select substring_index(flavor,'_',1), sum(purchase) from table group by 1

答案 1 :(得分:0)

在SQL 2005 +

可以使用以下内容:

Select Tbl1.Email, Count(1) FROM (
    Select substring(EmailAdd,CHARINDEX('@',EmailAdd),Len(EmailAdd)-CHARINDEX('@',EmailAdd)) as 'Email'
    from TableName 
) Tbl1 Group By Tbl1.Email