gpt4 book ai didi

mysql - 有没有办法将规范化数据选回一个结果集中

转载 作者:搜寻专家 更新时间:2023-10-30 20:30:57 25 4
gpt4 key购买 nike

我有三个表。 Foo、Attrib、FooAttrib。其中 FooAttrib 是 foo 和 attrib 之间的桥梁。如您所见,表中有许多 foo 和许多 attrib。

 Foo                 FooAttri                          Attrib
------------ ----------------------------- --------------
| id | Name | | fooId | attribId | value | | id | Descrip |
|----|-------| |---------|-----------|-------| |----|---------|
| 1 | Sam | | 1 | 1 | red | | 1 | Color |
| 2 | Bill | | 1 | 2 | Grape | | 2 | Flavor |
| 3 | Ted | | 2 | 1 | Blue | | 3 | Weight |
------------ | 3 | 3 | 10 | --------------
| 1 | 3 | 5 |
| 2 | 3 | 1 |
-----------------------------

我知道如何获得以下内容:

fooId   Name     Attrib     Value
1 Sam Color red
1 Sam Flavor Grape
1 Sam Weight 5
2 Bill Color Blue
2 Bill Weight 1
3 Ted Weight 10

但我想知道的是;是否可以构建一个 select 语句,以便我在一个结果集中返回 foo 和 attrib 数据,如下所示?

fooId   Name     Color   Flavor   Weight
1 Sam red Grape 5
2 Bill Blue 1
3 Ted 10

最佳答案

如果您知道在选定时间所需的所有可能参数,则可以这样做:

select f.id, f.name, fa_color.value as color, , fa_flavor.value as flavor
from foo f
join fooattri fa_color on f.id = fa_color.fooid and fa_color.attribid = 1
join fooattri fa_flavor on f.id = fa_flavor.fooid and fa_flavor.attribid = 2
...

关于mysql - 有没有办法将规范化数据选回一个结果集中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com