Mysql 使用两个不同的内部联接列选择colmun的名称和值';价值观
我不确定MySQL是否能够做这样的事情。我无论如何都不是MySQL大师,但对我来说,这似乎是一个透视表的例子 我看过其他透视表示例,我发现它们充其量也令人困惑,我仍然不确定它是否与我的情况相关 以下是有关数据和表格的示例: Foo表: Foo_元表: 下面是我正在使用的select的一个示例,但并不完全是我想要的: 选择: 下面是我试图选择的表格示例: 选择:Mysql 使用两个不同的内部联接列选择colmun的名称和值';价值观,mysql,pivot-table,Mysql,Pivot Table,我不确定MySQL是否能够做这样的事情。我无论如何都不是MySQL大师,但对我来说,这似乎是一个透视表的例子 我看过其他透视表示例,我发现它们充其量也令人困惑,我仍然不确定它是否与我的情况相关 以下是有关数据和表格的示例: Foo表: Foo_元表: 下面是我正在使用的select的一个示例,但并不完全是我想要的: 选择: 下面是我试图选择的表格示例: 选择: 是的,您正在旋转foo_元字段-至少Oracle的某些版本会这样做,我很确定MySQL不会这样做 通常,要在MySQL中“伪造”一个枢轴
是的,您正在旋转foo_元字段-至少Oracle的某些版本会这样做,我很确定MySQL不会这样做 通常,要在MySQL中“伪造”一个枢轴,需要进行多个连接。比如:
SELECT
t.col_one,
t.col_two,
t.col_three,
mthis.this,
mthat.that,
mother.other
FROM
foo t
INNER JOIN
(select fooid, MetaValue as This from foo_meta where MetaName = 'This') mthis ON t.RowID = mthis.FooID
INNER JOIN
(select fooid, MetaValue as That from foo_meta where MetaName = 'That') mthat ON t.RowID = mthat.FooID
INNER JOIN
(select fooid, MetaValue as Other from foo_meta where MetaName = 'Other') mother ON t.RowID = mother.FooID
根据数据的填充方式,您可能必须使用左外接器
RowID | FooID | MetaName | MetaValue
------|-------|----------|-----------
1 | 1 | This | 302
2 | 1 | That | 466
3 | 1 | Other | 132
4 | 2 | This | 222
5 | 2 | That | 87
6 | 2 | Other | 400
7 | 3 | This | 732
8 | 3 | That | 55
9 | 3 | Other | 690
SELECT
t.col_one,
t.col_two,
t.col_three,
m.MetaName,
m.MetaValue
FROM
foo t
INNER JOIN
foo_meta m ON t.RowID = m.FooID
RowID | col_one | col_two | col_three | This | That | Other
------|---------|---------|---------------------------------
1 | 2 | 34 | 64 | 302 | 466 | 132
2 | 6 | 53 | 23 | 222 | 87 | 400
3 | 8 | 22 | 45 | 732 | 55 | 690
SELECT
t.col_one,
t.col_two,
t.col_three,
mthis.this,
mthat.that,
mother.other
FROM
foo t
INNER JOIN
(select fooid, MetaValue as This from foo_meta where MetaName = 'This') mthis ON t.RowID = mthis.FooID
INNER JOIN
(select fooid, MetaValue as That from foo_meta where MetaName = 'That') mthat ON t.RowID = mthat.FooID
INNER JOIN
(select fooid, MetaValue as Other from foo_meta where MetaName = 'Other') mother ON t.RowID = mother.FooID