旋转概念
Hiii,
我的数据库设计如下:
Table File (FileID, Name, Details)
Table Attributes (AttID, AttName, AttType)
Table AttValues (FileID, AttID, AttValue)
直到运行时,才知道文件
中有多少个属性
以及名称。
我想在前端插入后以如下方式显示:
FileID, FileName, Details, (Rows of Attribute Table as Column here).
即, 任何人都可以给我提供一段 Java 或 MySQL 代码来实现这个透视结果。
非常感谢您抽出宝贵的时间。
或者有没有其他更好的方式来存储数据,以便我可以轻松获得想要的结果。
Hiii,
I have a Database design as:
Table File (FileID, Name, Details)
Table Attributes (AttID, AttName, AttType)
Table AttValues (FileID, AttID, AttValue)
Till the runtime, it is not known how many Attributes
are in a File
and of what names.
And I want to display after insertion at Frontend in a manner like like:
FileID, FileName, Details, (Rows of Attribute Table as Column here).
i.e.,
Can anybody provide me a piece of code in Java or in MySQL to achieve this Pivoting Result.
Highly thanks full for your precious time.
Or is there any other better way to store data, So that I can get the desired result easily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这需要两次查询。 首先选择
文件
:然后,获取
属性
:后一个查询将为每个
属性
提供一行,您可以通过编程方式对其进行透视显示在您的前端:根据需要适应您的本地编程环境。
当然,这仅适用于单个
File
或具有相同属性的File
。 如果您想显示具有不同Attributes
的多个文件,您可以预取所有AttName
:然后像这样加载值:
并使用本地关联数组从
进行映射>AttId
到列索引。作为最终的优化,您可以将最后两个查询合并到一个 OUTER JOIN 中,以避免第三次往返。 虽然这可能会增加传输的数据量,但如果您的类库支持命名列,它也会使填充表变得更容易。
This requires two queries. First select the
File
:Then, fetch the
Attributes
:The latter query will provide you with one row per
Attribute
, which you can programmatically pivot for display on your frontend:Adapt to your local programming environment as needed.
Of course, this only works for a single
File
orFile
s with the same attributes. If you want to display multiple files with differentAttributes
you can instead prefetch allAttName
s:Then load the values like this:
and use a local associative array to map from
AttId
s to column indexes.As a final optimisation, you can combine the last two queries into an
OUTER JOIN
to avoid the third round trip. While this will probably increase the amount of data transferred, it also makes filling the table easier, if your class library supports named columns.我最近回答了类似的问题:如何进行透视MySQL 实体-属性-值架构。 答案是 MySQL 特定的,但我想这没关系,因为问题是用 mysql 标记的。
I answered a similar question recently: How to pivot a MySQL entity-attribute-value schema. The answer is MySQL-specific, but I guess that's OK as the question is tagged with mysql.