對於不同條件,我需要對單列進行3次不同計數。單列不同條件下計數
表結構:
interview-
id-int(10)
c_id-int(10)
experience-varchar2(100)
經驗有3個不同的值 - 1)正 2)負 3)中性
我需要 「count_positive」, 「count_negative」 的3個不同的計數和「count_neutral」表示c_id = 10的條件。
我知道它可以通過3個不同的查詢。我可以通過單個查詢獲得3個計數嗎?
對於不同條件,我需要對單列進行3次不同計數。單列不同條件下計數
表結構:
interview-
id-int(10)
c_id-int(10)
experience-varchar2(100)
經驗有3個不同的值 - 1)正 2)負 3)中性
我需要 「count_positive」, 「count_negative」 的3個不同的計數和「count_neutral」表示c_id = 10的條件。
我知道它可以通過3個不同的查詢。我可以通過單個查詢獲得3個計數嗎?
SELECT
SUM(CASE experience
WHEN 'positive' THEN 1
ELSE 0
END) AS CountPositive
, SUM(CASE experience
WHEN 'negative' THEN 1
ELSE 0
END) AS CountNegative
, SUM(CASE experience
WHEN 'neutral' THEN 1
ELSE 0
END) AS CountNeutral
FROM Interview
WHERE c_id = 10
Weneger:最後一個ELSE條件是空的,我應該把它放在那裏。 –
@yaqubAhmad好,趕快,謝謝。我添加了0到最後一個'CASE' –
我可以獲得codeigniter的活動記錄查詢嗎? –
select 'Positive Count' , count(*)
from interview
where experience = 'positive'
UNION
select 'Negative Count' , count(*)
from interview
where experience = 'negative'
UNION
select 'Neutral' , count(*)
from interview
where experience = 'neutral'
你有沒有在問題中看到過這個陳述「我知道它可以通過3個不同的查詢」? –
這是亞當·溫格的回答修改後的版本:
SELECT
COUNT(CASE experience
WHEN 'positive' THEN 1
ELSE NULL
END) AS CountPositive
, COUNT(CASE experience
WHEN 'negative' THEN 1
ELSE NULL
END) AS CountNegative
, COUNT(CASE experience
WHEN 'neutral' THEN 1
ELSE NULL
END) AS CountNeutral
FROM Interview
WHERE c_id = 10
我可以獲得codeigniter的活動記錄查詢嗎? –
對不起,你能詳細說明一下嗎? –
我得到了解決方案。你可以檢查我的解決方案。 –
我覺得這工作得很好:
select 'count of ' + experience,
count(experience)
from interview
where c_id=10
group by experience
'+'不是MySQL中的連接運算符。 '||'是,但僅在ANSI SQL模式下。 – newtover
我可以獲得codeigniter的活動記錄查詢嗎? –
我得到了活動記錄查詢解決方案笨:
$this->db->select('SUM(CASE experience
WHEN "positive" THEN 1
ELSE 0
END) AS CountPositive
, SUM(CASE experience
WHEN "negative" THEN 1
ELSE 0
END) AS CountNegative
, SUM(CASE experience
WHEN "neutral" THEN 1
ELSE 0
END) AS CountNeutral');
$this->db->where('c_id',10);
$query=$this->db->get('interview');
$result=$query->result();
$interview_experience=$result[0];
$positive_count=$interview_experience->CountPositive;
$negative_count=$interview_experience->CountNegative;
$neutral_count=$interview_experience->CountNeutral;
我可以獲得codeigniter的活動記錄查詢嗎? –