0

堵塞 :

-------------
| id | name |
-------------
| 1  | test|
-------------

块关系坐标

-------------
| id | blockid| coordid
-------------
| 1  | 1   | 1
 -------------
| 1  | 1   | 2
 -------------
 | 1  | 1   | 3
 -------------

块坐标

    -------------
    | id | name| type
    -------------
    | 1  | North   | N
     -------------
    | 2  | East   | E
     -------------
     | 3  | South   | S
     -------------

现在我想加入这个表并在一行中得到这样的结果,结果表的标题也不太重要,只需从最后一个表中放入该字段

所有块表与坐标表有 3 个关系

    -------------
    name| NORTH | EAST | SOUTH
    -------------
    test | North | East | South
4

2 回答 2

2

尝试这个:

SELECT
  b.name,
  MAX(CASE WHEN type = 'N' THEN c.name END) AS North,
  MAX(CASE WHEN type = 'E' THEN c.name END) AS EAST,
  MAX(CASE WHEN type = 'S' THEN c.name END) AS SOUTH
FROM block AS b
INNER JOIN block_relation_coords AS r ON b.id = r.blockid
INNER JOIN block_coords          AS c ON c.id = r.coordid
GROUP BY b.name;

在这里查看它的实际效果:

这会给你:

| NAME | NORTH | EAST | SOUTH |
-------------------------------
| test | North | East | South |
于 2013-05-18T12:53:25.570 回答
1

您需要创建一个数据透视表...此页面为您提供了有关它的更多信息:

http://en.wikibooks.org/wiki/MySQL/Pivot_table

希望能帮助到你。

于 2013-05-18T12:46:24.863 回答