将查询结果中的主信息和明信息合并起来

发布于 2024-08-16 12:34:38 字数 460 浏览 4 评论 0原文

使用 openoffice.org Base 3.1.1

鉴于下面的数据库包含三个表,我想创建一个具有以下输出的查询。如何通过查询或视图来实现这一点?

输出

book.title, tags
title 1, tagdescription1 tagdescription2 tagdescription3
title 2, tagdescription2

数据库

BOOK
id - primary key,title
1, title 1
2, title 2

TAG
id - primary key,name
tag1, tagdescription1
tag2, tagdescription2
tag3, tagdescription3

BOOK_TAG
book_id,tag_id
1,tag1
1,tag2
1,tag3
2,tag2

Using openoffice.org Base 3.1.1

Given the database below with three tables, I would like to create a query with the following output. How can this be achieved with queries or views?

output

book.title, tags
title 1, tagdescription1 tagdescription2 tagdescription3
title 2, tagdescription2

database

BOOK
id - primary key,title
1, title 1
2, title 2

TAG
id - primary key,name
tag1, tagdescription1
tag2, tagdescription2
tag3, tagdescription3

BOOK_TAG
book_id,tag_id
1,tag1
1,tag2
1,tag3
2,tag2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2024-08-23 12:34:38

我的第一个想法是,由于列的数量是动态的,因此您无法真正获得这样的查询格式。我将连接表格,然后使用连接复制每个标签的标题。

<代码>
SELECT 书名、标签名称
来自书本
LEFT JOIN book_tag ON book_tag.book_id = book.id
LEFT JOIN 标签 ON tag.id = book_tag.tag_id

这应该给你一个结果集,比如,

book.title     tag.name
title 1        tagname1
title 1        tagname2
title 1        tagname3
title 2        tagname2

我相信有人有更好的方法:)

My first thought is that you can't really get a query to format like that as you have a dynamic amount of columns. I would join the tables and then have it duplicate the title for each tag using a join.


SELECT book.title, tag.name
FROM book
LEFT JOIN book_tag ON book_tag.book_id = book.id
LEFT JOIN tag ON tag.id = book_tag.tag_id

Which should, give you a resultset like,

book.title     tag.name
title 1        tagname1
title 1        tagname2
title 1        tagname3
title 2        tagname2

I'm sure someone has a better way though :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文