2013-07-16 4 views
0

Google 차트를 사용하여 파이 차트를 그립니다. 이를 그리기 위해 두 가지 열 형식의 데이터가 필요합니다. Google 차트 그리기 코드는 앞으로 구현 될 예정이므로 여기에 나와 있지 않습니다. 나는 다음과 같은 결과를 얻을 수 위의 쿼리를 실행하면여러 열의 쿼리 결과를 두 열로 변환하는 방법은 무엇입니까?

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


    ?> 

:

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

하지만 transactions_category라는 두 개의 열로 결과를 원하는 데이터베이스에서 데이터를 가져 오는 동안 나는 다음과 같은 코드를 작성했습니다 및 transaction_count. 이를 달성하기 위해 SQL 쿼리를 변경해야하는 경우 어떤 도움을 주시겠습니까? 미리 감사드립니다.

답변

1

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 연산자를 사용하여 쿼리에 대한이를보십시오.

+0

귀하의 검색어를 시도했지만 오류가 발생했습니다. 정확한 실행 코드를 알려주시겠습니까? 한 가지 더 주목해야 할 점은 쿼리의 모든 부분에서 FROM 테이블 이름을 언급하지 않았다는 것입니다. – PHPLover

+0

코드를 from 절로 업데이트했습니다 ... 이제는 lint가되었지만 테스트 할 테이블이 없습니다. – Orangepill

관련 문제