是否可以在mysql中将结果列分成两个

时间:2015-08-27 19:13:29

标签: php mysql

我不知道它是否可能在mysql中,但任何建议将不胜感激。另外,我通过我的PHP脚本调用此查询并在谷歌图表中使用结果。

我的查询返回以下结果。

mysql> SELECT substring_index(xo.module_name, '/', 1) as Module, COUNT(date) AS Count, Year(date) as Year FROM xclude_run xr, join_run_object jro, xclude_object xo WHERE xr.syshost='darter' AND xo.module_name IS NOT NULL AND xr.run_id = jro.run_id AND jro.obj_id = xo.obj_id GROUP BY Module, Year  ORDER BY Module, Year desc;
+--------------------------+-------+------+
| Module                   | Count | Year |
+--------------------------+-------+------+
| alps                     |    98 | 2015 |
| atp                      |  1220 | 2015 |
| atp                      |   279 | 2014 |
| boost                    |    28 | 2015 |
| cce                      | 10086 | 2015 |
| cp2k                     |     6 | 2014 |
| cray-hdf5                |    16 | 2014 |
| cray-hdf5-parallel       |   114 | 2015 |
| cray-libsci              |   721 | 2015 |
| cray-libsci              |   514 | 2014 |
| cray-mpich               |  1771 | 2015 |
| cray-mpich               |   386 | 2014 |
| cray-netcdf-hdf5parallel |    50 | 2015 |
| cray-netcdf-hdf5parallel |    76 | 2014 |
| cray-shmem               |  1832 | 2015 |
| cray-tpsl                |    29 | 2015 |
| cray-trilinos            |   104 | 2015 |
| craype-intel-knc         |  2676 | 2015 |
| craype-intel-knc         |   171 | 2014 |
| dmapp                    |   920 | 2015 |
| dmapp                    |     8 | 2014 |
| fftw                     |   288 | 2015 |
| fftw                     |  1791 | 2014 |
| gcc                      |  5371 | 2015 |
| gcc                      |   180 | 2014 |
| intel                    |    14 | 2015 |
| pmi                      |  2480 | 2015 |
| pmi                      |   416 | 2014 |
| rca                      |   920 | 2015 |
| udreg                    |  2479 | 2015 |
| udreg                    |   416 | 2014 |
| ugni                     |  2479 | 2015 |
| ugni                     |   416 | 2014 |
| wlm_detect               |    49 | 2015 |
| xpmem                    |  2479 | 2015 |
| xpmem                    |   416 | 2014 |
+--------------------------+-------+------+
36 rows in set (0.00 sec)

我希望结果能够以一种简洁的方式逐年显示,例如 -

+--------------------------+-------+------+-------+-------+
| Module                   | Count | Year | Count | Year |
+--------------------------+-------+------+-------+-------+
| alps                     |    98 | 2015 |     0 | 2014 |
| atp                      |  1220 | 2015 |   279 | 2014 |
| boost                    |    28 | 2015 |     0 | 2014 |
| cce                      | 10086 | 2015 |     0 | 2014 |
| cp2k                     |     0 | 2015 |     6 | 2014 |
| cray-hdf5                |     0 | 2015 |    16 | 2014 |
| cray-hdf5-parallel       |   114 | 2015 |     0 | 2014 |
| cray-libsci              |   721 | 2015 |   514 | 2014 |
| cray-mpich               |  1771 | 2015 |   386 | 2014 |
| cray-netcdf-hdf5parallel |    50 | 2015 |    76 | 2014 |
| cray-shmem               |  1832 | 2015 |     0 | 2014 |
| cray-tpsl                |    29 | 2015 |     0 | 2014 |
| cray-trilinos            |   104 | 2015 |     0 | 2014 |
| craype-intel-knc         |  2676 | 2015 |   171 | 2014 |
| dmapp                    |   920 | 2015 |     8 | 2014 |
| fftw                     |   288 | 2015 |  1791 | 2014 |
| gcc                      |  5371 | 2015 |   180 | 2014 |
| intel                    |    14 | 2015 |     0 | 2014 |
| pmi                      |  2480 | 2015 |   416 | 2014 |
| rca                      |   920 | 2015 |     0 | 2014 |
| udreg                    |  2479 | 2015 |   416 | 2014 |
| ugni                     |  2479 | 2015 |   416 | 2014 |
| wlm_detect               |    49 | 2015 |     0 | 2014 |
| xpmem                    |  2479 | 2015 |   416 | 2014 |
+--------------------------+-------+------+-------+------+

有没有办法在mysql中实现这一点? 谢谢你的建议。

这是最近的实施,因此我正在寻找2014年以后的数据。

1 个答案:

答案 0 :(得分:1)

由于PHP已添加到标记中,因此一种简单的方法是使用连接构建查询,这些连接遍历多年:

// Your query for a particular year
function originalQuery($currYear) {
    return "(SELECT substring_index(xo.module_name, '/', 1) as Module, 
    COUNT(date) AS Count, Year(date) as Year FROM xclude_run xr, join_run_object 
    jro, xclude_object xo WHERE xr.syshost='darter' AND xo.module_name IS NOT NULL
    AND xr.run_id = jro.run_id AND jro.obj_id = xo.obj_id GROUP BY Module, Year  
    ORDER BY Module HAVING year = $currYear) as q$currYear ";
}

// The joined query can be generated using a for loop for multiple years.
$joinedQuery = "SELECT * " . 
    "FROM " . originalQuery(2015) . " JOIN " . originalQuery(2014) .
    "ON q2015.Module = q2014.Module" .
    "ORDER by q2015.Module";