使用 SQL 链接多个表的结果

发布于 2024-11-16 18:25:12 字数 746 浏览 0 评论 0原文

我有一组具有以下结构的表现

**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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

压抑⊿情绪 2024-11-23 18:25:12

对于您当前的表结构,我认为以下内容可以工作

SELECT EntityFields.fid, CONCAT(L1.value, ' / ' L2.value)
FROM EntityFields INNER JOIN Language as L1 ON EntityFields.pid=L1.id and L1.type='Entity'
INNER JOIN Language as L2 ON EntityFields.fid=L2.id and L2.type='Field'
ORDER BY EntityFields.fid

但是,通过拥有更好的表结构可以使此查询变得更加容易。例如,对于以下结构:

**EntityFields**
fid | pid | uid
1   | 1   | 1
2   | 1   | 2
1   | 2   | 3
2   | 2   | 4
3   | 1   | 5

**Entities**
id | value
1  | FirstEntity
2  | SecondEntity

**Fields**
id | value
1  | Name
2  | Age
3  | Location

您可以使用稍微简单的查询:

SELECT uid, CONCAT(Entities.value, Fields.value)
FROM EntityFields INNER JOIN Entities ON EntityFields.pid=Entities.id
INNER JOIN Fields ON EntityFields.fid=Fields.id
ORDER BY uid

For your current table structure, I think the following will work

SELECT EntityFields.fid, CONCAT(L1.value, ' / ' L2.value)
FROM EntityFields INNER JOIN Language as L1 ON EntityFields.pid=L1.id and L1.type='Entity'
INNER JOIN Language as L2 ON EntityFields.fid=L2.id and L2.type='Field'
ORDER BY EntityFields.fid

However, this query could be made much easier by having a better table structure. For example, with the following structure:

**EntityFields**
fid | pid | uid
1   | 1   | 1
2   | 1   | 2
1   | 2   | 3
2   | 2   | 4
3   | 1   | 5

**Entities**
id | value
1  | FirstEntity
2  | SecondEntity

**Fields**
id | value
1  | Name
2  | Age
3  | Location

you can use the somewhat simpler query:

SELECT uid, CONCAT(Entities.value, Fields.value)
FROM EntityFields INNER JOIN Entities ON EntityFields.pid=Entities.id
INNER JOIN Fields ON EntityFields.fid=Fields.id
ORDER BY uid
不回头走下去 2024-11-23 18:25:12

好吧,我不知道你想在这里完成什么。事实上,您将一些记录标记为“实体”,将其他记录标记为“字段”,然后尝试将它们相互连接,这在我看来就像您在同一个表中混合了两个完全不同的事物。为什么没有实体表和字段表?

来获得您似乎想要的结果

select fid, le.value, lf.value
from entittyfields e
join language le on e.pid=le.id and type='Entity'
join language lf on e.fid=lf.id and type='Field'
order by fid

您可以通过编写“但我认为重新考虑您的表格设计” 。也许你可以解释一下你想要实现的目标。

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

select fid, le.value, lf.value
from entittyfields e
join language le on e.pid=le.id and type='Entity'
join language lf on e.fid=lf.id and type='Field'
order by fid

But I think you'd be wise to rethink your table design. Perhaps you could explain what you're trying to accomplish.

不弃不离 2024-11-23 18:25:12
SELECT ef.fid AS id
     , COALESCE(e.value, '-', ef.pid, ' / ', f.value)
       AS entity_field
FROM EntityFields ef
  JOIN Language AS e
    ON e.id = ef.id
    AND e.type = 'Entity'
  JOIN Language AS f
    ON f.id = ef.id
    AND f.type = 'Field'
ORDER BY ef.pid
       , ef.fid
SELECT ef.fid AS id
     , COALESCE(e.value, '-', ef.pid, ' / ', f.value)
       AS entity_field
FROM EntityFields ef
  JOIN Language AS e
    ON e.id = ef.id
    AND e.type = 'Entity'
  JOIN Language AS f
    ON f.id = ef.id
    AND f.type = 'Field'
ORDER BY ef.pid
       , ef.fid
勿忘心安 2024-11-23 18:25:12

如果我理解你的问题(我认为我不理解),那就很简单了。它似乎是一组设计非常糟糕的表格(例如,语言做不止一件事)。语言表似乎有两种类型的记录:a)实体记录,类型='Entity'和b)字段记录,类型='Field'。

无论如何,我处理它的方法是将语言表视为两个表:

select ef.fid, Entities.value, Fields.value
from entityfields ef
  inner join language Entities
    on Entities.id = ef.id
    and Entities.type = 'Entity'
  inner join language Fields
    on Fields.id = ef.id
    and Fields.Type = 'Field'
order by 2, 3

无论如何,先刺一下。这应该可以帮助你得到答案。

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:

select ef.fid, Entities.value, Fields.value
from entityfields ef
  inner join language Entities
    on Entities.id = ef.id
    and Entities.type = 'Entity'
  inner join language Fields
    on Fields.id = ef.id
    and Fields.Type = 'Field'
order by 2, 3

First stab, anyway. That should help you get the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文