我有一個Mysql查詢,從2個表中選擇一些內部連接的列。它應該計算來自一個表格列的「Y」值並用一個分組依據進行計數。MYSQL內部連接計數子查詢
這是我的查詢:
SELECT (SELECT Count(tblinterview.Contacted) FROM tblinterview WHERE tblinterview.Contacted = "Y")
as Contact_Y , tbldata.RegionID, tbldata.BranchCode, tbldata.BranchDesc, tbldata.CampaignID, tblinterview.EndTime
FROM tbldata INNER JOIN tblinterview
WHERE tblinterview.DataID = tbldata.DataId
AND tblinterview.Complete = "Y"
GROUP BY tbldata.BranchCode, tbldata.RegionID, tblinterview.EndTime
通過我的結果集爲: ,所有的Contact_Y是所有記錄,而不是分組計數的總數。
Contact_Y RegionID BranchCode BranchDesc CampaignID EndTime
1407 1 9725 Dada Motors 31 2012-01-26 12:51:12
1407 1 9725 Dada Motors 31 2012-01-31 10:25:18
1407 1 9725 Dada Motors 31 2012-02-01 11:30:37
1407 1 9725 Dada Motors 31 2012-02-01 14:48:57
1407 3 9732 Ballito Autohaus 37 2012-01-16 15:04:12
1407 3 9732 Ballito Autohaus 46 2012-01-18 11:59:53
1407 3 9732 Ballito Autohaus 31 2012-01-20 14:26:12
1407 3 9732 Ballito Autohaus 31 2012-01-20 16:05:48
有人能解釋我的查詢錯在哪裏嗎?
我們可以略過解釋,只是給你一個答案這是正確的? – Strawberry
@strawberry,我們的答案也將工作謝謝! :) – user1882752