简体   繁体   English

如何将查询结果从多列转换为两列?

[英]How to convert the query result from multiple columns to two columns?

I'm using Google Charts to draw pie chart. 我正在使用Google Charts绘制饼图。 For drawing it, the two column format of data is necessary. 为了进行绘制,数据的两列格式是必需的。 The code for drawing google charts is not given here as it's the part of future implementation. 此处未提供绘制Google图表的代码,因为这是将来实现的一部分。 For fetching the data from database I've written the following code: 为了从数据库中获取数据,我编写了以下代码:

<?php
    $con=mysql_connect("localhost","XYZ","pqrs") or die("Failed to connect with database!!!!");
    mysql_select_db("LMN", $con); 

    $sql  =" SELECT COUNT(*) 'carried_out', SUM(transaction_status = 'success') success, ";
    $sql .=" SUM(transaction_status = 'inprocess') inprocess, SUM(transaction_status = 'fail') fail, ";
    $sql .=" SUM(transaction_status = 'cancelled') cancelled FROM user_transaction GROUP BY transaction_status";

    $sth = mysql_query($sql) or die(mysql_error()); 

    /*$result  = mysql_fetch_array($sth, MYSQL_ASSOC); 
    print_r($result); die;*/
    $rows = array();
    //flag is not needed
    $flag = true;
    $table = array();
    $table['cols'] = array(

    // Labels for your chart, these represent the column titles
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
    array('label' => 'Transaction Category', 'type' => 'string'),
    array('label' => 'Percentage', 'type' => 'number')

);
//print_r($table);

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r['user_transaction']); 
//print_r($temp);
    // Values of each slice
    $temp[] = array('v' => (int) $r['transaction_count']); 
    //print_r($temp);

    $rows[] = array('c' => $temp);
    //print_r($rows);

}

$table['rows'] = $rows;

//print_r($table);

$jsonTable = json_encode($table);
//echo $jsonTable;


    ?>

If I execute the above query I get the following result: 如果执行上述查询,则会得到以下结果:

carried_out     success     inprocess   fail    cancelled
18  18  0   0   0
8   0   8   0   0
64  0   0   0   64

But I want the result into two columns named transactions_category and transaction_count. 但是我希望将结果分成两列,分别命名为transactions_category和transaction_count。 Can you help me what changes should I do the SQL query in order to achieve that? 您能帮我实现我的SQL查询的哪些更改吗? Thanks in advance. 提前致谢。

Try this for your query 尝试此查询

SELECT 'Success' as transactionType, count(*) from user_transaction where transaction_status = 'success';
UNION
SELECT 'In Process' as transactionType, count(*) from user_transaction where transaction_status = 'inprocess'
UNION
SELECT 'Fail' as transactionType, count(*) from user_transaction where transaction_status = 'fail'
UNION
SELECT 'Cancelled' as transactionType, count(*) from user_transaction where transaction_status = 'Cancelled';

Uses the union operator to combine the result sets from multiple queries into a single result set. 使用联合运算符将来自多个查询的结果集组合为一个结果集。

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

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