sql从视图中的4个表中选择

kt06eoxx  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(383)

这就是问题所在,我有4张table:

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,版本,编辑姓名和作者姓名。没有问题的名称编辑和秩序,但我不知道如何获得作者的名字。authory是书和作者中间的一个表。这是我目前的代码:

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'
)

选择到这一点是好的,但我如何才能添加作者的名字?例如a.name以获得如下表:

year    title   ISBN    edition   editorial_name    author_name
2011   somet    EC2       1          Pearson           George
2012    more    EC3       2          Pretince          Jeff
piztneat

piztneat1#

尝试以下方法,你必须加入 authorybooks 获取作者id idaut 然后加入 authoryauthor 获取作者姓名。

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
)
olhwl3o2

olhwl3o22#

你可以像普通table一样使用它
但您需要在视图中添加另一个列,即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 * 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';

正如草莓正确指出的那样,视图有一些限制,包括不能使用索引,因此当您直接使用索引时,它的速度可能会慢一些

相关问题