这是问题,我有这4个表。
书籍
idbook ISBN title sinopse pages lang code year edition idedit
1 EC2 somet thing 34 ES Ec2 2011 1 1
2 EC3 more things 545 ES Ec4 2012 2 2
3 EC4 lots ofthing 323 EN Uk1 2014 1 1
编辑部
idedit name country web
1 Pearson USA www.pearson.com
2 Pretince UK www.pretince.com
撰稿人
idaut name country bio
1 George USA lorem ipsum
2 Jeff UK dolor sit amet
作者
idbook idaut order
1 1 2
2 2 5
3 1 7
所以,我需要一个视图,只显示与ES lang相匹配的元素,按年份排序。对于每本书,你需要显示:年份,标题,ISBN,版本,编辑部名称和作者姓名。
create view `INFORMATICS` as
(select l.year, l.title, l.isbn, l.edition, e.name
from books l
inner join editorial e on e.idedit=l.idedit
where lang = 'ES'
)
我的代码是这样的: select到现在为止是很好的, 但是我怎么能添加作者的名字呢? 像a. name这样的表:
year title ISBN edition editorial_name author_name
2011 somet EC2 1 Pearson George
2012 more EC3 2 Pretince Jeff
试试下面的,你要加入 authory
与 books
获取作者ID idaut
再接 authory
与 author
来获取作者名。
create view `INFORMATICS` as
(select
l.year,
l.title,
l.isbn,
l.edition,
e.name,
a.name
from books l
inner join editorial e
on e.idedit=l.idedit
inner join authory ar
on l.idbook=ar.idbook
inner join author a
on ar.idaut=a.idaut
where lang = 'ES'
order by l.year
)
你可以像使用普通表格一样使用它
但你需要在视图中加入另一列,那就是idbook。
create view `INFORMATICS` as
(select l.idbook ,l.year, l.title, l.isbn, l.edition, e.name
from books l
inner join editorial e on e.idedit=l.idedit
where lang = 'ES'
)
然后SELECT和INNER JOIN
SELECT * FROM INFORMATICS i
INNER JOIN authory au
ON i.idbook=au.idbook
INNER JOIN author a
ON au.idaut=a.idaut
WHERE a.name ='Arthur';
正如草莓正确指出的 观点 有一些限制,包括不能使用索引,所以当你直接做这个的时候会比较慢。