CASE声明中的COUNT?

时间:2017-06-07 09:56:14

标签: mysql

我想根据一个字段的数量对记录进行分类。 我的代码是:

addSelect(\Illuminate\Support\Facades\DB::raw('TIME_FORMAT(start_time,"%H:%i") as st_time'))->orderBy('st_time','asc');

出于某种原因,我收到A为COUNT 1的记录,但对于COUNT大于1的记录为NULL。 post_id是一个INTEGER字段.. 任何帮助,将非常感谢!

2 个答案:

答案 0 :(得分:1)

CASE count(post_id) 
  WHEN count(post_id)=1 THEN 'A'

您正在将count(post_id)count(post_id)=1进行比较,其中后者导致true或false,在MySQL中等于1或0。你想要

CASE 
  WHEN count(post_id)=1 THEN 'A'

代替。

答案 1 :(得分:0)

尝试:::

select post_customer_fs_id AS 'post_customer_fs_id', count(post_id) AS 'open_post_count',
CASE 
    WHEN count(post_id)=1 THEN 'A'
    WHEN count(post_id)>1 THEN 'B'
    ELSE 'C'
END AS 'AAA'
from posts where post_status = '3' AND post_type_id= '1' 
group by post_customer_fs_id ;