我试图了解使用JOIN、COUNT(*)和GROUP BY进行非常简单的查询的正确方法。我实际上已经让它工作了(见下文),但从我读到的内容来看,我使用了GROUP BY不应该使用的额外内容。
(注意:下面的问题不是我的实际问题(它处理更复杂的表),但我试图提出一个类似的问题)
我有两张桌子:
Table: Person
-------------
key name cityKey
1 Alice 1
2 Bob 2
3 Charles 2
4 David 1
Table: City
-------------
key name
1 Albany
2 Berkeley
3 Chico
Run Code Online (Sandbox Code Playgroud)
我想对WHERE返回的 People (带有一些子句)进行查询
如果我做
SELECT COUNT(Person.key) AS count, City.key AS cityKey, City.name AS cityName
FROM Person
LEFT JOIN City ON Person.cityKey = City.key
GROUP BY Person.cityKey, City.name
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的结果
count cityKey cityName
2 1 …Run Code Online (Sandbox Code Playgroud) 是否有我可以做的SQL查询将生成一个线性序列
1, 2, 3, 4, 5, 6, 7 ... x+1
Run Code Online (Sandbox Code Playgroud)
要么
2, 7, 12, 17, 22 ... 2+5x
Run Code Online (Sandbox Code Playgroud)
(其中每个数字是结果表的一行中的一个条目)
I'd like to group and count the number of entries in a table that meet criteria colA <= x < colB
Suppose I had the following table:
index Game MinAgeInclusive MaxAgeExclusive -------------------------------------------------------- 1 Candy Land 3 8 2 Checkers 5 255 3 Chess 12 255 4 Sorry! 6 12 5 Monopoly 10 30
(this isn't what I'm doing, but it abstracts away a lot of the other complications with my setup)
Suppose I wanted to get a table that told …