SQL select 来自一个视图中的 4 个表
SQL select from 4 tables in a view
就是这个问题,我有这4个tables:
书籍
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 是 table ,位于书籍和作者中间。
到目前为止,这是我的代码:
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
要获得这样的 table:
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
)
你可以正常使用它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 和 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';
正如草莓正确指出的那样
views 有一些限制,包括不能使用索引,所以当你直接这样做时会更慢
就是这个问题,我有这4个tables:
书籍
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 是 table ,位于书籍和作者中间。 到目前为止,这是我的代码:
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 要获得这样的 table:
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
)
你可以正常使用它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 和 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';
正如草莓正确指出的那样 views 有一些限制,包括不能使用索引,所以当你直接这样做时会更慢