0

次の2つのテーブルがあります。

TABLE: area
*City_ID   *Number   Name
--------   -------   ----
SUR        1         Fleetwood
SUR        2         Whalley
SUR        3         Guildford
SUR        4         Newton
SUR        5         Cloverdale
SUR        6         South Surrey
ABB        1         Abbotsford East
ABB        2         Abbotsford West
ABB        3         Aberdeen
ABB        4         Bradner
ABB        5         Central Abbotsford
ABB        6         Matsqui
ABB        7         Poplar
ABB        8         Sumas Mountain
ABB        9         Sumas Prairie

TABLE: city
*ID        Name
---        ----
SUR        Surrey
ABB        Abbotsford
LAN        Langley

次のステートメントを使用します。

SELECT DISTINCT area.City_ID, city.Name
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID

私は得る:

SELECT:
City_ID    city.Name
-------    ---------
SUR        Surrey
ABB        Abbotsford

しかし、どうすれば次を選択できますか?

SELECT:
City_ID    city.Name    area_COUNT
-------    ---------    ----------
SUR        Surrey       6
ABB        Abbotsford   9

対応するarea_COUNT各行の行数はどこにありますか?areaCity_ID

4

4 に答える 4

2

GROUP BYの代わりに使用DISTINCT:

SELECT city.City_ID, city.Name, COUNT(*)
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID
GROUP BY city.ID
于 2012-07-17T08:49:30.097 に答える
1

Groupby句にNameも追加する必要があります

SELECT area.City_ID, city.Name, count(*) as area_COUNT 
FROM area 
INNER JOIN city 
WHERE area.City_ID = city.ID 
GROUP BY area.City_ID,City.Name
于 2012-07-17T08:55:07.690 に答える
0

DISTINCT の代わりに GROUP BY を使用します。

SELECT city.City_ID, city.Name, COUNT(*) FROM area INNER JOIN city WHERE area.City_ID = city.ID GROUP BY city.ID

于 2012-07-17T10:06:49.587 に答える
0

末尾に group by を追加

SELECT area.City_ID, city.Name, count(*) as area_COUNT
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID
GROUP BY area.City_ID
于 2012-07-17T08:49:32.907 に答える