使用GROUP BY从多个表中进行SELECT

时间:2012-09-16 13:49:40

标签: sql ms-access count group-by

我对多表查询有点问题。 (RDBMS:Access)

以下是数据库架构:(此查询中仅使用S_Cards,Books,Authors,Students表) S_Cards是学生用书订单(在库中)。

DB Scheme

查询: 选择学生中最受欢迎的作者以及在图书馆订购的作者书籍的编号。

虽然我可以在一个查询中获得订单列表+这样的作者:

SELECT 
    Students.FirstName & " " & Students.LastName AS [Student], 
    Books.Name AS [Book], Authors.FirstName & " " & Authors.LastName AS [Author]
FROM 
    Students, 
    Books, 
    S_Cards, 
    Authors
WHERE 
    S_Cards.ID_Student = Students.ID 
AND S_Cards.ID_Book = Books.ID 
AND Books.ID_Author = Authors.ID
ORDER BY Authors.LastName

结果(对不起,用俄语):

Query result

我无法弄明白,为什么我不能像这样COUNT和GROUP BY:

SELECT 
    Students.FirstName & " " & Students.LastName AS [Student], 
    Books.Name AS [Book], 
    COUNT(Authors.FirstName & " " & Authors.LastName) AS [Number of books]
FROM Students, Books, S_Cards, Authors
WHERE 
    S_Cards.ID_Student = Students.ID 
AND S_Cards.ID_Book = Books.ID 
AND Books.ID_Author = Authors.ID
GROUP BY 3

我收到一个'Authors.FirstName& “”& Authors.LastName'不是静态函数或组的一部分。

问题:

  1. 有没有办法在没有JOIN的情况下执行此查询,只有GROUP BY,SELECT,UNION以及如何执行此操作?
  2. 我的第二个查询中出现了什么问题?

3 个答案:

答案 0 :(得分:1)

解决方案(摘自older revision in question):

SELECT TOP 1 Author, COUNT(Book) AS [Number of books] FROM
(
    SELECT 
        Students.FirstName & " " & Students.LastName AS [Student], 
        Books.Name AS [Book], 
        Authors.FirstName & " " & Authors.LastName AS [Author]
    FROM 
        Students, 
        Books, 
        S_Cards, 
        Authors
    WHERE 
        S_Cards.ID_Student = Students.ID AND
        S_Cards.ID_Book = Books.ID AND
        Books.ID_Author = Authors.ID
    ORDER BY Authors.LastName
)
GROUP BY Author
ORDER BY 2 DESC

答案 1 :(得分:0)

SQL Server中的字符串concatenation operator+而不是&。此外,您应该按照聚合函数的内容进行分组。

SELECT Students.FirstName + ' ' + Students.LastName AS [Student]
     , Books.Name AS [Book]
     , COUNT(Authors.FirstName + ' ' + Authors.LastName) AS [Number of books]
  FROM Students
  JOIN S_Cards
    ON S_Cards.ID_Student = Students.ID
  JOIN BOOKS
    ON S_Cards.ID_Book = Books.ID 
  JOIN Authors
    ON Books.ID_Author = Authors.ID
 GROUP BY Students.FirstName + ' ' + Students.LastName
        , Books.Name

请注意,我已将您的查询更改为标准的ANSI连接语法,这会使错误变得更加困难并且更容易阅读。

想一想,你的计数似乎有点奇怪。不是书籍的数量COUNT(Books.ID)

答案 2 :(得分:0)

您必须GROUP BY任何不属于聚合函数的东西:

SELECT 
    Students.FirstName & " " & Students.LastName AS [Student], 
    Books.Name AS [Book], 
    COUNT(Authors.FirstName & " " & Authors.LastName) AS [Number of books]
FROM Students, Books, S_Cards, Authors
WHERE 
    S_Cards.ID_Student = Students.ID 
AND S_Cards.ID_Book = Books.ID 
AND Books.ID_Author = Authors.ID
GROUP BY Students.FirstName & " " & Students.LastName, 
    Books.Name AS [Book]

我建议你开始使用显式连接而不是隐式连接。在大多数情况下,MS Access有更好的选择。

<...>
FROM Students
INNER JOIN S_Cards
ON Students.ID = S_Cards.ID_Student

或者

<...>
FROM Students
LEFT JOIN S_Cards
ON Students.ID = S_Cards.ID_Student

查询设计窗口允许您使用正确的语法构建JOIN。只需将连接字段从一个表拖放到下一个表,然后选择所需的连接类型即可。