mysql根据check

时间:2017-03-01 07:56:12

标签: php 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);

如果我打印$ 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 个答案:

答案 0 :(得分:1)

您可以使用CASE WHEN进行多重选择

  $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");