MySQL,如何将多个记录连接成一个?

3
我将尝试从WordPress数据库中获取信息;我需要的数据分散在三个表中:
  • wp_em_events
  • wp_em_locations
  • wp_postmeta
下面是一个简化的图示:
wp_em_events
+-----+--------+-----+
| id  | loc_id | a   |
+-----+--------+-----+
|   1 | 3      | foo |
|   2 | 4      | bar |
+-----+--------+-----+

wp_em_locations
+--------+----+
| loc_id | b  |
+--------+----+
|    3   | x  |
|    4   | y  |
+--------+----+

wp_postmeta
+-------+------+------+
|post_id| key  |value |
+-------+------+------+
|    1  | name | dave |
|    1  | age  | 14   |
|    2  | name | rob  |
|    2  | age  | 20   |
+-------+------+------+

$querystr = "
    SELECT *
    FROM wp_em_events
    LEFT JOIN wp_em_locations ON wp_em_events.loc_id = wp_em_locations.loc_id
    LEFT JOIN wp_postmeta ON wp_em_events.id = wp_postmeta.post_id
    WHERE wp_em_events.id = 1
    GROUP BY wp_em_events.location_id, wp_em_events.id
";

这将返回:
+-----+--------+-----+----+-----+-------+
| id  | loc_id | a   |  b | key | value |
+-----+--------+-----+----+-----+-------+
|   1 | 3      | foo |  x | age |  20   |
+-----+--------+-----+----+-----+-------+

我的GROUP BY破坏了名称->dave,我希望得到:

+-----+--------+-----+----+-----+-----+
| id  | loc_id | a   |  b | name| age |
+-----+--------+-----+----+-----+-----+
|   1 | 3      | foo |  x | dave|  14 |
+-----+--------+-----+----+-----+-----+

我需要捕获所有的元记录,并以某种方式将它们与父记录关联起来,最好是作为它们所代表的键值对。

wp_em_events.post_id 在您的样本数据中不存在。 - Conrad Frix
谢谢,这只是假设,因为实际表格细节更加混乱。我只是在寻找任何一种方法来保留所有的“元”记录并将它们与“事件”相关联,而不需要进行递归查询。 - Sinetheta
我认为您不需要递归查询,只需要两个即可。与@Darfs的答案类似,但MySQL版本不太好。 - Conrad Frix
2个回答

3

Well you could do this..

SELECT e.id, 
       e.loc_id, 
       e.a, 
       l.b, 
       p_name.Value `Name`, 
       p_age.Value  Age 
FROM   wp_em_events e 
       INNER JOIN wp_em_locations l 
         ON e.loc_id = l.loc_id 
       INNER JOIN wp_postmeta p_name 
         ON e.id = p_name.Post_id 

       INNER JOIN wp_postmeta p_age 
         ON e.id = p_age.Post_id 

WHERE  e.id = 1 
            AND p_name.`key` = 'name' 
            AND p_age.`key` = 'age' 

演示

但您需要动态构建查询。具体来说,是列名和别名以及WHERE子句。


我现在还不能在没有谷歌组件的情况下剖析它,但看起来输出是我想要的,结构也更符合我的预期。非常感谢! - Sinetheta

0
做一个动态数据透视表。这里有一个我在其中一个项目中使用的示例。虽然它与您的情况没有直接关联,但您可以理解要点。
create table testowy_pivot
            (
             name varchar(255),
             color varchar(255),
             quantity int
            )

insert into testowy_pivot (name, color, quantity) values ('road frames','black',null)
insert into testowy_pivot (name, color, quantity) values ('road frames','red',null)
insert into testowy_pivot (name, color, quantity) values ('helmets','red',288)
insert into testowy_pivot (name, color, quantity) values ('helmets','black',324)
insert into testowy_pivot (name, color, quantity) values ('helmets','blue',216)
insert into testowy_pivot (name, color, quantity) values ('socks','white',180)
insert into testowy_pivot (name, color, quantity) values ('socks','white',216)


DECLARE @columns VARCHAR(8000)

SELECT
@columns = COALESCE(@columns + ',[' + cast(color as varchar) + ']','[' + cast(color as varchar)+ ']')
FROM testowy_pivot
GROUP BY color


DECLARE @query VARCHAR(8000)

SET @query = '
           SELECT *
                     FROM testowy_pivot
                               PIVOT
                                  (
                           sum(quantity) --sum, max, etc.
                               FOR [color]
                               IN (' + @columns + ')
                                  )
                     AS p'

EXECUTE(@query)

select * from testowy_pivot

敬礼,m.


如果是SQL Server,那么可以。但是MySQL则不太行。 - Conrad Frix
正确 - 这是SQL,但MySQL中的数据透视表也是可行的。http://en.wikibooks.org/wiki/MySQL/Pivot_table - darf
谢谢,我会尝试一下。对于记录保留中我想象中的一个相当标准的问题来说,这似乎是一个非常复杂的解决方案。 - Sinetheta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接