MySQL,如何将多条记录加入到一条记录中

时间:2012-05-10 21:13:31

标签: mysql sql wordpress

我正在尝试从wordpress数据库获取信息;我需要的数据分布在3个表之间:

  • 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 |
+-----+--------+-----+----+-----+-----+

我需要捕获所有元记录,并以某种方式将它们与父记录相关联,最好是它们的key->值。

2 个答案:

答案 0 :(得分:3)

嗯,你可以这样做..

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' 

DEMO

但是你需要动态构造查询。特别是列名和别名以及WHERE子句

答案 1 :(得分: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

的问候, 米。