问题12.找出拥有或借用该书“978-”的学生的姓名。 0262033848’ 。使用 UNION 。
select s.name
from student s, loan l
where s.email = l.borrower and l.book = '978-0262033848'
union
select s.name
from student s, copy c
where s.email = c.owner and c.book = '978-0262033848';
问题13.找出拥有或借用该书“978-”的学生的姓名。 0262033848’ 。使用 OR 。
我该怎么做?
答案 0 :(得分:0)
您可以将这三个表连接在一起,然后在OR
子句中使用WHERE
条件来获得所需的结果集:
SELECT DISTINCT s.name
FROM student s
LEFT JOIN loan l
ON s.email = l.borrower
LEFT JOIN copy c
ON s.email = c.owner
WHERE l.book = '978-0262033848' OR
c.book = '978-0262033848';
您还会注意到我使用显式左连接替换了您的隐式连接,将连接条件从WHERE
移动到ON
子句。这是最佳做法,因为它使您的查询更易于阅读和维护。
<强>更新强>
我给你的查询是这样做的首选方式。如果你想坚持使用ANSI-92之前的表格,你可以试试这个:
SELECT DISTINCT s.name
FROM student s, loan l, copy c
WHERE s.email = l.borrower OR
s.email = c.owner AND
(l.book = '978-0262033848' OR
c.book = '978-0262033848')
答案 1 :(得分:0)
select s.name
from student s
left join loan as l on s.email = l.borrower
left join copy as c on s.email = c.owner
where l.book = '978-0262033848' or c.book = '978-0262033848'