首页 > mysql 语句求教

mysql 语句求教

我有一个表,其中有两个字段source和target,其中,不同的source可以到达相同的target。我想统计一下对于每个target,可以到达这个target的source的数量。 比如有如下数据:

source    target
1             a
2             a
1             a
3             a
1             b
2             b
2             b
3             b

统计出来的结果应该是:

source    count  target
1          2       a
2          1       a
3          1       a
1          1       b
2          2       b
3          1       b    

请问可以用一条sql语句搞定吗?我查了一下,用group by和count(*),但是结果不对,求教


看着应该很简单啊.

select source, count(*) count,  target 
from table1 group by target, source;
【热门文章】
【热门文章】