MySQL查询DISTINCT,COUNT

时间:2014-09-05 21:54:43

标签: mysql count distinct

我试图使用SELECT DISTINCT从这张桌子上找出有多少人拥有汽车,有多少人有船,现在有很多自行车等等

|   name   |   obj  |
---------------------
|  john    |  car   |
|  mary    |  boat  |
|  dave    |  car   |
|  james   |  bike  |
|  steve   |  car   |
|  walt    |  bike  |

ex:bike - 2

2 个答案:

答案 0 :(得分:1)

您可以使用此查询

SELECT obj, COUNT(name) as nb FROM table GROUP BY obj ORDER BY obj

结果(sqlfiddle):

|   obj    |   nb   |
---------------------
|  bike    |    2   |
|  boat    |    1   |
|  car     |    3   |

答案 1 :(得分:0)

select sum(obj = 'car') as car_count,
       sum(obj = 'boat') as boat_count
from your_table