问题是:
“对于给定的主题,列出借用该主题的所有书籍的会员名称和生日。”
我已经尝试过了:
SELECT
t.label, m.name, m.tel
FROM
Theme t, Member m, Book b, Copies c, Borrow b1
WHERE
t.themecode = b.themecode
AND b.bookid = c.bookid
AND b1.copyid = c.copyid
AND b1.mid = m.mid
AND t.label = 'Action'
,它返回了所有借用该主题书籍的会员,但我只希望借用该主题的所有书籍的会员
答案 0 :(得分:1)
您有一份详细的会员列表以及他们为某个主题拥有的所有借贷。
您想查看成员借了多少本不同的书。
您还希望查看给定主题的总共不同的书籍。
您只想列出借用了不同主题书籍的成员与主题主题的书籍总数之和。
SELECT m.name,m.tel
FROM Theme t,Member m, Book b,Copies c,Borrow b1
WHERE t.themecode=b.themecode AND
b.bookid=c.bookid AND
b1.copyid=c.copyid AND
b1.mid=m.mid AND
t.label='Action'
Group by m.name, m.tel-- distinct users
Having count(distinct bookid) = -- number of distinct books each person borrowed
(Select count(distinct bookid) from Theme t, Book b WHERE t.themecode=b.themecode and t.label= 'Action') --total number of distinct books for your Theme
注释/答案也是正确的,您应该使用显式联接。我只是在帮助您将逻辑添加到脚本中,并解释其工作原理。
答案 1 :(得分:0)
您可以考虑通过swaggerValidator.init('test/unit-tests/input-validation/pet-store-swagger.yaml')
.then(function () {
const app = express();
app.use(bodyParser.json());
app.get('/pets', swaggerValidator.validate, function (req, res, next) {
return res.json({ result: 'OK' });
});
app.post('/pets', swaggerValidator.validate, function (req, res, next) {
return res.json({ result: 'OK' });
});
app.get('/pets/:petId', swaggerValidator.validate, function (req, res, next) {
return res.json({ result: 'OK' });
});
app.use(function (err, req, res) {
if (err instanceof swaggerValidator.InputValidationError) {
return res.status(400).json({ more_info: JSON.stringify(err.errors) });
}
});
// const server = app.listen(serverPort, function () {});
});
将HAVING
子句与COUNT
聚合合并表:
LEFT JOIN
答案 2 :(得分:0)
使用CTE获取主题为“操作”的图书数量。
正确加入所有表格,按成员分组并计算他们借用的不同书籍的数量,并将其与CTE的结果进行比较:
with cte as(
select count(*) counter from book where themecode = (
select themecode from theme where label = 'Action'
)
)
select t.label, m.name, m.birthdate
from member m
inner join borrow br on br.mid = m.mid
inner join copies c on c.copyid = br.copyid
inner join book b on b.bookid = c.bookid
inner join theme t on t.themecode = b.themecode
where t.label = 'Action'
group by t.label, m.mid, m.name, m.birthdate
having count(distinct b.bookid) = (select counter from cte)