作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有三个表。 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/
我是一名优秀的程序员,十分优秀!