【发布时间】:2018-07-08 03:52:24
【问题描述】:
我在大学里上 SQL 课,我们正在使用 PSQL。
供参考的表格。
Table "public.author"
Column | Type | Modifiers
------------+---------+-----------
au_id | numeric | not null
first_name | text | not null
last_name | text | not null
year_born | numeric | not null
Table "public.book"
Column | Type | Modifiers
--------+---------+-----------
title | text | not null
year | numeric | not null
isbn | text | not null
pub_id | numeric | not null
我遇到的问题是: 显示从 1990 年到 1993 年(含)出版的所有书籍的作者姓名、书名和出版年份。将作者姓名显示为 Last, First(中间有逗号和空格)。按出版年份对输出进行排序。
输出:
select concat(last_name, ',', first_name) as name, title, year from author, book where year >= 1990 and year <= 1993 group by year order by year;
错误:列“author.last_name”必须出现在 GROUP BY 子句中或在聚合函数中使用 第 1 行:选择 concat(last_name, ',', first_name) 作为 name, title, ye...
我知道它说我需要按顺序排列,但问题要求我按年份范围订购。
【问题讨论】:
-
你为什么还要使用
GROUP BY?我看不出有必要。但是您缺少author和book之间的连接条件。而且您可能更应该使用显式的author INNER JOIN book ON ...语法——更容易阅读、更容易理解并且错误会很明显。 -
@stickybit 我使用的是
GROUP BY,因为到目前为止我们在课堂上所做的一切都让我相信这是必要的。我们还没有涵盖INNER JOIN,所以我不确定它是否适用于这个问题(你不可能知道)。我取下了GROUP BY,终端现在挂了:select concat(last_name, ',', first_name) as name, title, year from author, book where year >= 1990 and year <= 1993 order by year; -
没有链接
author和book的键。你不能用这两个表做你想做的事。
标签: sql postgresql group-by