用php传播mysql数据

时间:2017-04-15 06:56:05

标签: php mysql

我还没有在这里发布,但我一直在使用该网站。多年来,我找到了我需要的东西或修改的例子。谢谢你。也许我今晚很累,但是我需要使用PHP对MySQL数据进行一些格式化。

我当前的查询返回如下所示的数据:

 league_id  | complex_location        |  complex_id  | complex_field

00000000015 | Savage Sports Complex   |  00000000001 | Savage South

00000000015 | Savage Sports Complex   |  00000000001 | Savage North

我使用以下查询来获取上述格式的数据:

查询:

select a.league_id
     , b.complex_location
     , b.complex_id
     , c.complex_field 
  from wp_lm_leagues a 
  left 
  join wp_lm_sports_complex b 
      on b.complex_id in(a.complex_id_1,a.complex_id_2 a.complex_id_3,a.complex_id_4,a.complex_id_5) 
left 
join wp_lm_sports_complex_fields c
  on b.complex_id = c.complex_id 
where a.league_id = 15

我希望数据转换如下:

league_id  | complex_location      | complex_id  | complex_field | complex_field2

00000000015| Savage Sports Complex | 00000000001 | Savage South  | Savage North

1 个答案:

答案 0 :(得分:0)

使用GROUP_CONCAT()函数。试试这个查询

SELECT a.league_id, b.complex_location, b.complex_id, 
GROUP_CONCAT(c.complex_field SEPARATOR ', ') AS complex_field FROM wp_lm_leagues AS a
LEFT JOIN wp_lm_sports_complex AS b ON 
(a.complex_id_1 = b.complex_id or a.complex_id_2 = b.complex_id or 
 a.complex_id_3 = b.complex_id or 
a.complex_id_4 = b.complex_id or a.complex_id_5 = b.complex_id) 
LEFT JOIN wp_lm_sports_complex_fields AS c ON b.complex_id = c.complex_id
WHERE a.league_id = 00000000015 GROUP BY a.league_id
 ORDER BY a.league_id, b.complex_location