什么 SQL 查询或视图将显示“动态列”?
我有一个数据表,我允许人们向该表添加元数据。
我为他们提供了一个界面,允许他们将其视为向存储数据的表添加额外的列,但实际上我将数据存储在另一个表中。
Data Table
DataID
Data
Meta Table
DataID
MetaName
MetaData
因此,如果他们想要一个存储数据、日期和名称的表,那么我将在数据表中包含数据,在元名称中包含“日期”一词,在元数据中包含日期,以及数据表中的另一行元表,元名称中包含“名称”,元数据中包含名称。
我现在需要一个查询,从这些表中获取信息,并将其呈现为来自带有两个附加列“数据”和“名称”的单个表,因此对于客户来说,看起来就像有一个带有自定义列的表:
MyTable
Data
Date
Name
或者,换句话说,我该如何从这里:
Data Table
DataID Data
1 Testing!
2 Hello, World!
Meta Table
DataID MetaName MetaData
1 Date 20081020
1 Name adavis
2 Date 20081019
2 Name mdavis
到这里:
MyTable
Data Date Name
Testing! 20081020 adavis
Hello, World! 20081019 mdavis
几年前,当我使用 PHP 在 MySQL 中执行此操作时,我做了两个查询,第一个查询获取额外的元数据,第二个查询将它们连接在一起。 我希望现代数据库有处理这个问题的替代方法。
与此问题的选项 3 相关。
-亚当
I have a table of data, and I allow people to add meta data to that table.
I give them an interface that allows them to treat it as though they're adding extra columns to the table their data is stored in, but I'm actually storing the data in another table.
Data Table
DataID
Data
Meta Table
DataID
MetaName
MetaData
So if they wanted a table that stored the data, the date, and a name, then I'd have the data in the data table, and the word "Date" in metaname, and the date in MetaData, and another row in the meta table with "Name" in metaname and the name in metadata.
I now need a query that takes the information from these tables and presents it as though coming from a single table with the two additional columns "Data" and "Name" so to the customer it would look like there's a single table with their custom columns:
MyTable
Data
Date
Name
Or, in other words, how do I go from here:
Data Table
DataID Data
1 Testing!
2 Hello, World!
Meta Table
DataID MetaName MetaData
1 Date 20081020
1 Name adavis
2 Date 20081019
2 Name mdavis
To here:
MyTable
Data Date Name
Testing! 20081020 adavis
Hello, World! 20081019 mdavis
Years ago when I did this in MySQL using PHP, I did two queries, the first to get the extra meta data, the second to join them all together. I'm hoping that modern databases have alternate methods of dealing with this.
Related to option 3 of this question.
-Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要旋转 MyTable 中的每个名称-值对行...尝试以下 sql:
You want to pivot each of your name-value pair rows in the MyTable... Try this sql:
您可能需要添加一个附加子句(AND Data = 'some value')来返回用户感兴趣的行。
You'll probably want to add an additional clause (AND Data = 'some value') to return the rows the user is interested in.
AFAIK,您只能使用动态
SQL
存储过程在服务器端执行此操作。实际上,您想要动态生成的代码是:
下面是执行此操作的代码:
您可以使用类似的动态技术,使用
CASE
语句示例来执行数据透视。AFAIK, you can do this on the server-side only with a dynamic
SQL
stored procedure.Effectively the code you want to generate dynamically is:
And here's code to do it:
You could use a similar dynamic technique using the
CASE
statement example to perform the pivot.