加入2个表的查询 [英] Join Query on 2 tables

查看:101
本文介绍了加入2个表的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tables:
Book: BID,BNAME,BPRICE,SID
Author: AID,ANAME,BID

Question:
Write query which give list of Books, having more than one Author.







这个问题在采访中被问到了我。




This question was asked to me in interview.

推荐答案

如果你不知道,那么你不值得这份工作。



你能看到两个表都在列中的列吗?这是BID。所以sql是



If you didn't know, then you didn't deserve the job.

Can you see a column that both tables have in column ? It's BID. So the sql is

select b.bname, a.aname from book b inner join author a on b.bid = a.bid





获取所有列表作者和书籍。但是,你想要的是拥有多个作者的书籍。所以答案是:





to get a list of all authors and books. However, what you want, is books having more than one author. So the answer is:

select a.aname, count(b.bname) from book b inner join author a on b.bid = a.bid group by a.aname having count(b.bname) > 1


这篇关于加入2个表的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆