[英]How to create/add a column as a return output in an SQL select query based on another column's values?
[英]How to create/add a column in an SQL select query based on another column's values?
我想根據條件通過SQL select查詢動態添加另一個列hook_name
。
例如,如果hook_type = 0
,則表hook_name
的值應為OFFER
,類似於hook_type = 1
, hook_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;
提前致謝。
使用標准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
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
BTW我想你想要轉義列名hook name
。 然后使用反引號而不是引號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.