Table-1 (bk_order_details): order_id, order_line, book_id, quantity, order_price
Table-2 (bk_books): book_id, title
Table-3 (bk_book_topics): book_id, topic_id
问题:
显示某人订购的图书的图书ID和标题,该图书被分类为SQL图书和数据库图书。
使用topic_id过滤DB和SQL。
按book_id排序。
注意:仅允许使用子信息。
提前谢谢!我尝试了多种方法,但没有得到理想的结果。
答案 0 :(得分:1)
如果这些topic_id是数字,那么您需要知道所需的topic_id数字。
或者它需要一个额外的表格,从那些' SQL'和' DB'类型可以选择。
F.e。
Table-4 (bk_topics): topic_id, topic_type
在这种情况下,下面的SQL只使用连接和子查询:
select
b.book_id,
b.title
from bk_book_topics bt
join
(
select topic_id
from bk_topics
where (topic_type = 'SQL' or topic_type = 'DB')
group by topic_id
) t on (t.topic_id = bt.topic_id)
join bk_order_details o on (o.book_id = bt.book_id)
join bk_books b on (b.book_id = o.book_id)
group by b.book_id, b.title
order by b.book_id
如果topic_id不是数字,而是具有这些类型的varchar?
如果允许使用子查询,但没有义务?
然后它可以高尔夫编码:
select
b.book_id,
b.title
from bk_book_topics bt
join bk_order_details o on (o.book_id = bt.book_id)
join bk_books b on (b.book_id = o.book_id)
where bt.topic_id in ('SQL','DB')
group by b.book_id, b.title
order by b.book_id