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

Php 如何将查询结果从多列转换为两列?,php,mysql,database,arrays,Php,Mysql,Database,Arrays,我用谷歌图表画饼图。要绘制它,需要两列格式的数据。这里没有给出绘制谷歌图表的代码,因为这是未来实现的一部分。为了从数据库中获取数据,我编写了以下代码: <?php $con=mysql_connect("localhost","XYZ","pqrs") or die("Failed to connect with database!!!!"); mysql_select_db("LMN", $con); $sql =" SELECT COUNT(*) 'car

我用谷歌图表画饼图。要绘制它,需要两列格式的数据。这里没有给出绘制谷歌图表的代码,因为这是未来实现的一部分。为了从数据库中获取数据,我编写了以下代码:

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


    ?>

但我希望将结果分为两列,分别命名为transactions\u category和transaction\u count。您能否帮助我,为了实现这一点,我应该对SQL查询进行哪些更改?提前谢谢

为您的查询尝试此选项

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';

使用union运算符将多个查询的结果集合并到单个结果集中

我尝试了你的查询,但它给出了错误。你能给我一个查询的可运行代码吗?我注意到的另一件事是,您在查询中的任何地方都没有提到FROM表名。使用FROM子句更新了代码。。。它现在起毛了,但我没有你的桌子来测试它。
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';