19

我想hook_name根据条件通过 SQL 选择查询动态添加另一列。

例如,如果hook_type = 0, tablehook_name的值应该是OFFER,同样对于hook_type = 1hook_name应该显示“ACCEPT”。

下面是结果的截图:

在此处输入图像描述

选择查询是这样的:

select hook_type, 'hook name' as hook_name,
       count(*) as number_of_exchange_activities 
from `exchange` 
group by hook_type # hook_type 0 for OFFER, 1 for ACCEPT and 2 for offer EXPIRED;

提前致谢。

4

2 回答 2

29

使用标准 SQL 案例:

SELECT hook_type, 
   CASE hook_type
      WHEN 0 THEN 'OFFER'
      WHEN 1 THEN 'ACCEPT'
      WHEN 2 THEN 'EXPIRED'
   END AS hook_name,
   COUNT(*) AS number_of_exchange_activities 
FROM `exchange` 
GROUP BY hook_type
于 2013-08-19T11:19:59.153 回答
8
select case when hook_type = 0 then 'offer'
            when hook_type = 1 then 'accept'
            else 'expired'
       end as hook_t, 
      `hook name` as hook_name,
      count(*) as number_of_exchange_activities 
from `exchange` 
group by hook_t

顺便说一句,我认为您想转义列名hook name。然后使用反引号而不是引号。

于 2013-08-19T11:17:13.830 回答