使用 SQL 链接多个表的结果
我有一组具有以下结构的表现
**EntityFields**
fid | pid
1 | 1
2 | 1
3 | 2
4 | 2
5 | 1
**Language**
id | type | value
1 | Entity | FirstEntity
2 | Entity | SecondEntity
1 | Field | Name
2 | Field | Age
3 | Field | Name
4 | Field | Age
5 | Field | Location
在您可能已经理解,第一个表为每个实体提供了 EntityField 分配。第二个表给出了这些 ID 的名称。我想要输出类似于以下内容
1 | FirstEntity / Name (i.e. a concat of the Entity and the EntityField name)
2 | FirstEntity / Age
3 | FirstEntity / Location
4 | SecondEntity / Name
5 | SecondEntity / Age
这可能吗?
感谢您的回答,不幸的是表结构是我无法更改的。它本身的表结构属于另一个数据目录系统,该系统非常灵活,我用它来提取数据。我知道,如果不提供必要的背景,这个表结构看起来很奇怪,但它运行得很好(除了在这种情况下)。
我将尝试这里的示例并让您知道。
I have a set of tables with following structures
**EntityFields**
fid | pid
1 | 1
2 | 1
3 | 2
4 | 2
5 | 1
**Language**
id | type | value
1 | Entity | FirstEntity
2 | Entity | SecondEntity
1 | Field | Name
2 | Field | Age
3 | Field | Name
4 | Field | Age
5 | Field | Location
Now as you may have understood, the first table gives the EntityField assignment to each Entity. The second table gives out the names for those IDs. What I want to output is something like the following
1 | FirstEntity / Name (i.e. a concat of the Entity and the EntityField name)
2 | FirstEntity / Age
3 | FirstEntity / Location
4 | SecondEntity / Name
5 | SecondEntity / Age
Is this possible?
Thank you for the answers, unfortunately the table structure is something that I cannot change. The table structure it self belongs to another data directory system which is quite flexible and which I am using to pull out data. I know that without providing the necessary background, this table structure looks quite weird, but it is something that works quite well (except in this scenario).
I will try out the examples here and will let you know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于您当前的表结构,我认为以下内容可以工作
但是,通过拥有更好的表结构可以使此查询变得更加容易。例如,对于以下结构:
您可以使用稍微简单的查询:
For your current table structure, I think the following will work
However, this query could be made much easier by having a better table structure. For example, with the following structure:
you can use the somewhat simpler query:
好吧,我不知道你想在这里完成什么。事实上,您将一些记录标记为“实体”,将其他记录标记为“字段”,然后尝试将它们相互连接,这在我看来就像您在同一个表中混合了两个完全不同的事物。为什么没有实体表和字段表?
来获得您似乎想要的结果
您可以通过编写“但我认为重新考虑您的表格设计” 。也许你可以解释一下你想要实现的目标。
Well, I have no idea what you're trying to accomplish here. The fact that you label some records "Entity" and others "Field" and then try to connect them to each other makes it look to me like you are mixing two totally different things in the same table. Why not have an Entity table and a Field table?
You could get the results you seem to want by writing
But I think you'd be wise to rethink your table design. Perhaps you could explain what you're trying to accomplish.
如果我理解你的问题(我认为我不理解),那就很简单了。它似乎是一组设计非常糟糕的表格(例如,语言做不止一件事)。语言表似乎有两种类型的记录:a)实体记录,类型='Entity'和b)字段记录,类型='Field'。
无论如何,我处理它的方法是将语言表视为两个表:
无论如何,先刺一下。这应该可以帮助你得到答案。
If I understand your question, which I don't think I do, this is simple. It appears to be a set of very poorly designed tables (Language doing more than one thing, for example). And it appears that the Language table has two types of records: a) The Entity records, which have type='Entity' and b) Field records, which have type='Field'.
At any rate, the way I would approach it is to treat the Language table as if it were two tables:
First stab, anyway. That should help you get the answer.