MySQL:将 1:m 的所有值放入一个字段?
假设以下可能的模型:
模型 1:
TABLE: book
- book_id
- name
TABLE: book_author
- book_author_id
- book_id
- author_id
TABLE: author
- author_id
- name
(一本书可以有 0 个或多个作者)
模型 2:
TABLE: book
- book_id
- name
TABLE: book_eav
- book_eav_id
- book_id
- attribute (e.g. "author")
- value (e.g. "Tom Clancy")
(一本书可以有 0 个或多个作者 + 有关出版商的信息,页数等)
如何查询以获取 book.book_id
、book.name
+ an authors
包含 0 个或多个与 书?
我正在尝试这样做,以便可以将查询结果用作 Lucene 索引中的文档。
Assume the following possible models:
Model 1:
TABLE: book
- book_id
- name
TABLE: book_author
- book_author_id
- book_id
- author_id
TABLE: author
- author_id
- name
(a book can have 0 or more authors)
Model 2:
TABLE: book
- book_id
- name
TABLE: book_eav
- book_eav_id
- book_id
- attribute (e.g. "author")
- value (e.g. "Tom Clancy")
(a book can have 0 or more authors + information about publisher, number of pages, etc.)
How do I query this in such a way that I get back book.book_id
, book.name
+ an authors
field that contains the 0 or more values associated with the book?
I'm trying to do this so I can use the query results as documents in a Lucene index.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 GROUP_CONCAT 函数。
http://dev.mysql.com/doc/refman /5.0/en/group-by-functions.html
Take a look to the GROUP_CONCAT function.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Lucene 支持多个同名字段,这意味着您实际上可以在同一个文档上存储多个名为“Author”的字段。
我建议您实际上这样做,并使用另一个语句获取其他数据,例如:
对书籍进行选择。
一项选择 book_id 和作者。
Lucene supports multiple fields with the same name, meaning that you can actually store multiple fields named "Author" on the same document.
I suggest that you actually do that, and fetch additional data using another statement, for example:
one select for books.
one select for book_id and the author.