简体   繁体   English

从mysql表获取数据

[英]fetch data form mysql table

hello all below is my query and its giving fine results 你好,下面是我的查询,它给出了很好的结果

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

but what if i want results like below is there any solution any help would be appriciated thanks in advance ... 但是,如果我想得到如下所示的结果,有什么解决方案可以帮助您吗?

date             Sydney         Melbourne      Brisbane     Perth       

1973-09-30       2.50000        5.70000        6.60000      7.10000

below is my table structure 下面是我的表结构

datadatexnumericy(first table)

series_id     x            y
43532        1991-12-31   -2.10000

don't confuse about series_id because city name is coming from collection series table where series_id matches and fetches city name 不要对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`

I can't think of a way right now to query it in such fashion but you could restructure the normal fetched results first. 我现在无法想到以这种方式查询它的方法,但是您可以首先重组正常的获取结果。 Build it with that specialized format first, then present it. 首先以该专用格式构建它,然后呈现它。

First is to get the headers (the dates and places etc.), then you need to group the body data according to dates, push them inside another container. 首先是获取标题(日期和地点等),然后您需要根据日期对正文数据进行分组,并将其推入另一个容器中。

Rough example: 粗略的例子:

<?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
}

?>

Then, once the structure is made, then you present it, (as you normally would) in a loop: 然后,一旦构造好结构,然后就可以按循环显示(通常)了:

<!-- 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 样例输出

If its for a known set of chart_names , then you can use the following technique for generating the pivot table 如果它是一组已知的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 

You can also add the where condition before the group by as 您还可以在group by之前添加where条件

WHERE cs.collection_id =  '265'

Here is how you can make it dynamic 这是使它动态化的方法

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;

Check the demo here 此处查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM