我有一个表recommendation,其字段为recommenderattractionid

我想通过attractionid计算表组中存在多少attractionid,但是如果recommenderattractionid对相同,则将它们计为1。
例如,

attractionid    recommender
      1               1
      1               2
      1               1
      2               3
      2               1
      2               2
      2               2
      2               2


预期结果 :

 attractionid   count
      1            2
      2            3


下面的行应计为1

 attractionid    recommender
          1               1
          1               1
          2               2
          2               2
          2               2

最佳答案

distinct attractionid,recommender函数中使用count

询问

select attractionid,
count(distinct attractionid,recommender) as `count`
from recommendation
group by attractionid;

10-08 11:20