2015-04-03 70 views
0

大家好以下是我的查询和它给优秀成果获取数据形式MySQL表

SELECT `collection_series`.`chart_name`, `datadatexnumericy`.`x` as date, `datadatexnumericy`.`y` 
FROM (`datadatexnumericy`) 
JOIN `collection_series` ON `collection_series`.`series_id` = `datadatexnumericy`.`series_id` 
WHERE `collection_series`.`collection_id` = '265' 

chart_name   date    y 

Sydney    1973-09-30   2.50000 
Melbourne   1973-09-30   5.70000 
Brisbane   1973-09-30   6.60000 
Perth    1973-09-30   7.10000 

但如果我要像下面的结果是没有什么解决任何帮助将appriciated在此先感谢...

date    Sydney   Melbourne  Brisbane  Perth  

1973-09-30  2.50000  5.70000  6.60000  7.10000 
下面

是我的表结构

datadatexnumericy(first table) 

series_id  x   y 
43532  1991-12-31 -2.10000 

不要混淆约series_id因为城市名称从收藏系列表,其中series_id比赛和获取城市名

collection_series(second table) 

in this table there is coloumn which name is collection_id and series_id 
collection id is '265' and i am matching `collection_series`.`series_id` = `datadatexnumericy`.`series_id` 
+0

你可以分享你的表结构前添加where条件具有完整的测试数据。此外,请发布您的完整代码。 – 2015-04-03 12:41:46

+0

刚刚更新了表结构 – 2015-04-03 12:49:47

回答

1

如果其对于一组已知的chart_names,则可以使用以下技术来生成数据透视表

select 
dd.x as date, 
max(case when cs.chart_name = 'Sydney' then dd.y end) as `Sydney`, 
max(case when cs.chart_name = 'Melbourne' then dd.y end) as `Melbourne`, 
max(case when cs.chart_name = 'Brisbane' then dd.y end) as `Brisbane`, 
max(case when cs.chart_name = 'Perth' then dd.y end) as `Perth` 
from datadatexnumericy dd 
join collection_series cs on cs.series_id = dd.series_id 
group by dd.x 

您也可以在group by作为

WHERE cs.collection_id = '265' 

这里是你如何可以使动态

set @sql = NULL; 
select 
    group_concat(distinct 
    concat(
     'max(case when cs.chart_name = ''', 
     cs.chart_name, 
     ''' then dd.y end) AS ', 
     replace(cs.chart_name, ' ', '') 
    ) 
) INTO @sql 
from collection_series cs 
join datadatexnumericy dd on cs.series_id = dd.series_id 
; 

set @sql = concat('select dd.x as date, ', @sql, ' from datadatexnumericy dd 
join collection_series cs on cs.series_id = dd.series_id 
group by dd.x'); 

prepare stmt from @sql; 
execute stmt; 
deallocate prepare stmt; 

检查demo here

+0

谢谢,但当城市名称来自数据库和城市名称可以改变,如果用户想要....比我怎样才能使它动态 – 2015-04-03 13:06:05

+0

因此,你可能有多少个城市,如果是这样的话,那么同样的查询需要使用动态sql编写,而预编译语句本质上更复杂。 – 2015-04-03 13:08:17

+0

所以你可以请帮我... – 2015-04-03 13:09:36

1

我不能想办法现在就查询它在这样的方式,但你可以先重组正常获取的成果来。首先用专门的格式构建它,然后呈现它。

首先获取标题(日期和地点等),然后您需要根据日期对正文数据进行分组,然后将它们推入另一个容器中。

粗糙例如:

<?php 
// temporary container 
$temp = array(); 
while($row = whatever_fetch_function_assoc($result)) { 
    $temp[] = $row; // push the rows 
} 

// restructure 
$places = array_column($temp, 'chart_name'); // if this is not available (only PHP 5.5) 
// foreach($temp as $v) { 
// $places[] = $v['chart_name']; // if its not available just use foreach 
// } 
// header creation 
$headers = array_merge(array('Date'), $places); // for headers 
foreach($temp as $v) { // then extract the dates 
    $data[$v['date']][] = $v['y']; // group according to date 
} 

?> 

然后,一旦结构制成,则提交,(象通常那样)在一个循环中:

<!-- presentation --> 
<table cellpadding="10"> 
    <thead> 
     <tr><?php foreach($headers as $h): // headers ?> 
     <th><?php echo $h; ?></th> 
     <?php endforeach; ?></tr> 
    </thead> 
    <tbody> 
    <?php foreach($data as $date => $values): ?> 
     <tr> 
      <td><?php echo $date; // the date ?></td> 
      <?php foreach($values as $d): ?> 
      <td><?php echo $d; ?></td> 
      <?php endforeach; ?> 
     </tr> 
    <?php endforeach; ?> 
    </tbody> 
</table> 

Somewhat of a sample output

+0

等什么?我误解了这个问题吗?他不是只想按日期分组吗?就是这样吗? – Loko 2015-04-03 12:55:06

+0

感谢您的关注,让我检查一下我的结局...... – 2015-04-03 12:58:58

+0

@Ghost是否有任何修改查询,我必须做出或罚款? – 2015-04-03 13:00:17