MySQL - 如何旋转 NVP?
假设我有一个包含以下行的 product_attribute
表:
================================================================
| product_attribute_id | product_id | name | value |
================================================================
| 1 | 25 | Author | John Doe |
----------------------------------------------------------------
| 2 | 25 | Author | Jane Doe |
----------------------------------------------------------------
| 3 | 55 | Publisher | ABC Corp |
----------------------------------------------------------------
| 4 | 55 | Release Date | 20100125 |
----------------------------------------------------------------
我正在考虑实现 Solr 进行全文搜索,并且我认为该表可能包含应该建立索引的重要信息。因此,我认为该表需要进行透视(使用 product_id
作为透视点),以便我可以将其与具有应索引信息的其他表结合起来。
问题:
- 如何在 MySQL 中进行透视?
- 我事先不知道所有名称/值对将会是什么。这会是一个问题吗?
- 某些属性具有相同的名称(例如上例中的“作者”)。这会是一个问题吗?
Say I have a product_attribute
table with the following rows:
================================================================
| product_attribute_id | product_id | name | value |
================================================================
| 1 | 25 | Author | John Doe |
----------------------------------------------------------------
| 2 | 25 | Author | Jane Doe |
----------------------------------------------------------------
| 3 | 55 | Publisher | ABC Corp |
----------------------------------------------------------------
| 4 | 55 | Release Date | 20100125 |
----------------------------------------------------------------
I'm looking into implementing Solr for full-text searching and I think this table potentially has important information that should be indexed. So, I think this table needs to be pivoted (using product_id
as the pivot point) so I can combine it with other tables that have information that should be indexed.
Questions:
- How do I pivot this in MySQL?
- I do not know in advance what all the name/value pairs are going to be. Will this be a problem?
- Some attributes have identical names (e.g. "Author" in the example above). Will this be a problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个非常标准的实现,
你必须这样做
,这样你就可以构建上面的查询
但不,它不能使用相同的名称,GROUP_CONCAT 将连接这些值。
我见过一个实现,它添加一列并用增量值填充它,以便它可以使用变量和计数器旋转表。但我在 mysql 中没有这个
thats a pretty standard implementation
you have to
so you can build the above query
but NO it will not work with identical names , GROUP_CONCAT will concat the values .
i ve seen an implementation which adds a column and populates it with increment values so that it can then pivot the table using variables and a counter. but i dont have that in mysql