【发布时间】:2011-03-14 14:39:00
【问题描述】:
当我运行查询号 1 时:
SELECT id, name, COUNT(name) AS count
FROM markers
WHERE state like 'OR'
group by named
我得到以下结果:
id name count
14 Alsea 2
76 Applegate Valley 1
3 Ashland 9
64 Beaver 1
26 Bend 1
10 Carlton 2
这很好,除了我希望计数名称的每个实例都显示其各自的 ID,如查询号 2 的结果所示:
SELECT id, name, COUNT(name) AS count
FROM markers
WHERE state like 'OR'
group by name, id
这会产生以下结果:
id name count
14 Alsea 1
28 Alsea 1
76 Applegate Valley 1
3 Ashland 1
13 Ashland 1
16 Ashland 1
20 Ashland 1
22 Ashland 1
43 Ashland 1
48 Ashland 1
51 Ashland 1
72 Ashland 1
64 Beaver 1
26 Bend 1
10 Carlton 1
27 Carlton 1
我是否可以运行一个查询,它将返回来自查询号 2 的 id 和名称以及来自查询号 1 的每行中的相应计数,如下所示?
id name count
14 Alsea 2
28 Alsea 2
76 Applegate Valley 1
3 Ashland 9
13 Ashland 9
16 Ashland 9
20 Ashland 9
22 Ashland 9
43 Ashland 9
48 Ashland 9
51 Ashland 9
72 Ashland 9
64 Beaver 1
26 Bend 1
10 Carlton 2
27 Carlton 2
【问题讨论】: