如何基于另一个列的值在SQL选择查询中创建/添加列? [英] How to create/add a column in an SQL select query based on another column's values?
本文介绍了如何基于另一个列的值在SQL选择查询中创建/添加列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据条件通过SQL选择查询动态添加另一列hook_name
.
I want to dynamically add another column hook_name
, via an SQL select query, based on a condition.
例如,如果hook_type = 0
,表hook_name
的值应为OFFER
,并且对于hook_type = 1
,hook_name
的值应显示为"ACCEPT".
For example if hook_type = 0
, table hook_name
should have a value of OFFER
, and similarly for hook_type = 1
, hook_name
should show "ACCEPT".
下面是结果的屏幕截图:
Below is a screenshot of the result:
选择查询是这样的:
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;
提前谢谢.
推荐答案
使用标准的SQL CASE:
Use a Standard SQL CASE:
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
这篇关于如何基于另一个列的值在SQL选择查询中创建/添加列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文