从mysql表中获取数据

时间:2015-04-03 12:39:55

标签: php 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

但是,如果我想要下面的结果,有什么解决方案可以提前感谢任何帮助...

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

不要混淆seri_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`

2 个答案:

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

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

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