2017-03-01 35 views
1

這裏是我的代碼MySQL的替換特定的文本列基於檢查

$all = array(); 

$pdo = new PDO("mysql:host=$hostname;dbname=$dbname;charset=utf8", $username, $password); 
$stmt = $pdo->query("SELECT `resume_status` , count(`resume_status`) total_candidates 
FROM `conts` WHERE `client_id` = '56' group by `resume_status`"); 

$all[] = $stmt->fetchAll(PDO::FETCH_ASSOC); 

echo json_encode($all); 

如果我打印$所有的數組沒有JSON編碼它,它顯示了以下數據

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [resume_status] => 1 
        [total_candidates] => 39 
       ) 

      [1] => Array 
       (
        [resume_status] => 2 
        [total_candidates] => 7 
       ) 

      [2] => Array 
       (
        [resume_status] => 3 
        [total_candidates] => 2 
       ) 

     ) 

) 

我取從表中的多個狀態針對該resume_status柱

如果resume_status = 1(那麼它應該是resume_status => '待處理')

如果resume_status = 2(那麼它應該是resume_status> '在隊列')

如果resume_status = 3(那麼它應該是resume_status => '已批准')

我已經在試圖case語句查詢但沒有成功。任何幫助都感激不盡。感謝

回答

1

你可以使用CASE WHEN多的choise

$stmt = $pdo->query("SELECT 
     CASE when `resume_status` = 1 then 'Pending' 
      when `resume_status` = 2 then 'In Queue' 
      when `resume_status` = 3 then 'Approved' 
     END as str_resume_status, count(`resume_status`) total_candidates 
    FROM `conts` 
    WHERE `client_id` = '56' 
    group by str_resume_status"); 
+0

謝謝.. 曾任職完美:) –