我有一张这样的桌子。
----------------------------
| ID | CITY | BLOCK| NAME |
----------------------------
| 1 |Jakarta| A | John |
| 2 |Jakarta| A | Rey |
| 3 |Bekasi | A | Boy |
----------------------------
正确的查询计算城市是雅加达的街区总数是什么?如果雅加达有相同的街区,这些街区应该算作一个?
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 1 |
-----------------------
如果我使用此查询,
SELECT COUNT(block) FROM member AS total_block_jakarta WHERE city = 'Jakarta' GROUP BY block
它会像这样返回多行。
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 1 |
| 1 |
-----------------------
如果我在上面的查询中使用COUNT(DISTINCT)
,它将返回这样的值。
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 2 |
-----------------------
请帮我找到正确的查询。感谢。
答案 0 :(得分:1)
试试这个
SELECT COUNT(DISTINCT block) AS total_block_jakarta FROM MEMBER WHERE CITY = 'Jakarta';
答案 1 :(得分:0)
您不需要对该块进行分组,因为您指的是雅加达有多少成员使用相同的块。
修改强>
SELECT COUNT(DISTINCT(block)) FROM member AS total_block_jakarta WHERE kota = 'Jakarta'
答案 2 :(得分:0)
SELECT COUNT(DISTINCT BLOCK) AS total_block_jakarta FROM member WHERE CITY LIKE "Jakarta"
输出:
--------------------
total_block_jakarta
1
---------------------