如何在mysql中获取计数,相同的值

时间:2018-03-16 10:16:39

标签: mysql sql

这里我想把计数当作表中相同的值,假设我的表中的例子Accomodation是两次,所以,住宿计数应该是2,剩余计数是1

  

getting_fecilities

fid      eventId           fecilityName

 1          5                 Accomodation
 2          5                 Breakfast
 3          5                 Lunch
 4          5                 Dinner
 5          6                 Food(VEG)
 6          5                 Parking
 7          5                 Accomodation
  

MYSQL

SELECT `fecilityName`,COUNT(*) AS count FROM getting_fecilities WHERE eventId=5 group by `fecilityName`
  

我的结果

fecilityName  count

Accomdation    1
Accomdation    1
Breakfast      1
Dinner         1
Lunch          1
Parking        1
  

我的预期结果

fecilityName  count

Accomdation    2
Breakfast      1
Dinner         1
Lunch          1
Parking        1

2 个答案:

答案 0 :(得分:1)

您的查询正在给出预期结果。 请在以下链接中查看:

  

http://sqlfiddle.com/#!9/ba6457/4

答案 1 :(得分:0)

    SELECT `fecilityName`, COUNT(fecilityName) AS count 
    FROM getting_fecilities WHERE eventId = 5 
    group by `fecilityName`;