2013-06-03 58 views
5

我有4個表ACCOUNTS_TABLELINKS_TABLEGROUPS_TABLEKEYS_TABLE enter image description here如何寫連接查詢4個表中的以下條件

enter image description here

enter image description here

enter image description here

我需要獲得all accounts details這是acct_type xxcount of Links, groups& keywords。我曾嘗試此查詢,但它給所有count as 0

SELECT 
    acc.acct_id, acc.acct_type, count(link.id) as link_count, link.account, 
    groups.camp_id, count(groups.id) as group_count, count(keyword.key_id) as key_count 

FROM ".ACCOUNTS_TABLE." as acc 
    LEFT JOIN ".LINKS_TABLE." as link ON link.account=acc.acct_id AND acct_type='xx' 
    LEFT JOIN ".GROUPS_TABLE." as groups ON groups.camp_id=link.id 
    LEFT JOIN ".KEYS_TABLE." as keyword ON keyword.camp_id=link.id 

GROUP BY acc.acct_id 

我需要的輸出應該是這樣的 enter image description here

任何一個請幫我解決售後服務這個問題

回答

1

您可能應該使用COUNT(DISTINCT ....)。

SELECT acc.acct_id, COUNT(DISTINCT link.id), COUNT(DISTINCT groups.id), COUNT(DISTINCT keyword.key_id) 
FROM ACCOUNTS_TABLE acc 
LEFT OUTER JOIN LINKS_TABLE link ON link.account = acc.acct_id AND acct_type = 'advertiser' 
LEFT OUTER JOIN GROUPS_TABLE groups ON groups.camp_id = link.id 
LEFT JOIN KEYS_TABLE keyword ON keyword.id = link.id 
WHERE acc.acct_type = 'xx' 
GROUP BY acc.acct_id 

編輯

修訂,使用本更新的加盟條件等: -

SELECT acc.acct_id, acc.acct_type, COUNT(DISTINCT link.id) , COUNT(DISTINCT groups.id) , COUNT(DISTINCT keyword.key_id) 
FROM ACCOUNTS_TABLE acc 
LEFT OUTER JOIN LINKS_TABLE link ON link.account = acc.acct_id 
LEFT OUTER JOIN GROUPS_TABLE groups ON groups.camp_id = link.id 
LEFT JOIN KEYS_TABLE keyword ON keyword.camp_id=link.id 
WHERE acc.acct_type = 'xx' 
GROUP BY acc.acct_id, acc.acct_type 
+0

仍然它的所有計數爲零 – Juice

+0

當我省略GROUP BY acc.acct_id它給總數正確。但我需要分開所有帳戶! – Juice

+0

似乎阻止它將有用數據帶回測試數據的事情是,您已經在連接表上指定了一個「廣告客戶」的acct_type。沒有匹配的記錄。刪除這些會得到一些記錄。您還指定了使用key_id和鏈接表id加入密鑰表。你想加入這些或在camp_id這些會更有意義嗎? – Kickstart

0

你可以嘗試這樣的事:

SELECT ACC.Id 
     ,(SELECT COUNT (*) FROM Links L WHERE L.AccountId = ACC.Id) AS CountOfLinks 
     ,(SELECT COUNT (*) FROM Groups G WHERE G.AccountId = ACC.Id) AS CountOfGroups 
FROM (SELECT Id FROM Accounts Acc WHERE Acc.Type = 'some type') ACC 
+0

爲什麼子查詢? –

+1

Co相關的子查詢非常昂貴。 –

+0

@OlivierCoilland:你說得對,你完全可以沒有。 – souplex

0
SELECT 
    accounts_table.acct_id, 
    accounts_table.acct_type, 
    COUNT(DISTINCT links_table.id) AS link_count, 
    COUNT(DISTINCT groups_table.id) AS group_count, 
    COUNT(DISTINCT keys_table.key_id) AS key_count 
FROM 
    accounts_table 
LEFT JOIN 
    links_table 
    ON links_table.account = accounts_table.acct_id 
LEFT JOIN 
    groups_table 
    ON groups_table.camp_id = links_table.id 
LEFT JOIN 
    keys_table 
    ON keys_table.camp_id = links_table.id 
WHERE 
    acct_type = 'xx' 
GROUP BY 
    accounts_table.acct_id, 
    accounts_table.acct_type 
ORDER BY 
    link_count DESC, 
    group_count DESC, 
    key_count DESC 

編輯答案以匹配更新的問題 - 這應該做你所要求的。

這應該做你問什麼,SQL小提琴這裏 - http://www.sqlfiddle.com/#!2/f4b6a/20

+0

這是我給出的相同查詢。 – Juice

+0

更新SQL現在 –

0

我rejigged你的代碼位(見下文)的幾個原因:

  1. 這是有幫助的(對我總是以某種方式編寫我的SELECT語句 - 將所有未分組的東西放在第一位,理想情況下按照我的JOIN放置相同的順序,並在GROUP BY中執行相同的操作。
  2. 我將任何限制我的FROM表進入WHERE而不是JOIN來使它更清楚我想要做什麼和a以便以後更容易修改。
  3. 我也希望確保它能夠更容易地掃描問題。

把這個重新安排的查詢,並通讀它,以確保你得到你期望的行爲。

PS我不確定你的表名和引號樣式 - 我通常使用back ticks(`),並且永遠不會把點(。)放在我的表名中。如果你把它們當作佔位符來使用,那麼它們可能會給你帶來麻煩,如果它們是真實的。

SELECT 
acc.acct_id, 
-- if you don't group by these then you need to remove them as they will just return the first values based on mysql behaviour 
acc.acct_type, 
link.account, 
groups.camp_id, 
-- these counts will only count where an ID is present which seems like what you're after 
count(link.id) as link_count, 
count(groups.id) as group_count, 
count(keyword.key_id) as key_count 
FROM ".ACCOUNTS_TABLE." as acc 
LEFT JOIN ".LINKS_TABLE." as link ON link.account=acc.acct_id 

LEFT JOIN ".GROUPS_TABLE." as groups ON groups.camp_id=link.id 
LEFT JOIN ".KEYS_TABLE." as keyword ON keyword.id=link.id 

WHERE acct_type='advertiser' 

GROUP BY acc.acct_id, 
-- only use these if you intend to group by them 
acc.acct_type, 
link.account, 
groups.camp_id DESC 
0
SELECT acct_type, 
     count(acct_type), 
     count(l.id), 
     count(g.id), 
     count(key_id) 
FROM accounts a 
LEFT JOIN links l ON (l.account = a.acct_id) 
LEFT JOIN groups g ON (g.camp_id = l.id) 
LEFT JOIN keysTable k ON k.group_id = g.id 
GROUP BY acct_type HAVING acct_type = 'xx'; 

SQL小提琴驗證:http://www.sqlfiddle.com/#!2/f4b6a/20

+0

我需要將所有帳戶的詳細信息分開計算。它只是給總數。即acc1有2個鏈接1個組和3個關鍵字,acc2有3個鏈接3個組和1個關鍵字,如wise – Juice