关系数据库包含两个表:图书和作者,每个表包含100多万行。
表被描述为:书籍(标题,出售),其中标题是主要的主要作者(作者,标题),其中标题指的是书籍
编写一个查询,根据销售的拷贝数显示前3名作者和最后3名作者。
我正在努力解决这个问题,但是我失败了。
select *
from authors a
left join books b on a.authortitle = b.title
order by b.sold;当我尝试这个,这是我收到的回应。
select *
from authors a
left join books b on a.authortitle = b.title
order by b.sold
group by author;错误1064 (42000):您的SQL语法出现了错误;请检查与您的MySQL服务器版本对应的手册,以便在第1行使用接近“按作者分组”的语法。
我不知道这是什么意思。有人知道如何解决这个问题吗?谢谢!
发布于 2019-04-22 08:42:33
您可以使用UNION运算符。类似这样的代码(此代码未经测试:D )
(select * from authors a left join books b on a.authortitle = b.title order by b.sold limit 3)
union
(select * from authors aa left join books bb on aa.authortitle = bb.title order by bb.sold desc limit 3)发布于 2019-04-22 09:32:49
select a.*,
rank() over(partition by a.author order by b.copiessold asc) rk
from authors a
left join books b on a.authortitle = b.title
where rk<4
UNION ALL
select a.*,
rank() over(partition by a.author order by b.copiessold desc) rk
from authors a
left join books b on a.authortitle = b.title
where rk<4;https://stackoverflow.com/questions/55791371
复制相似问题