5

我有 4 张桌子ACCOUNTS_TABLE, LINKS_TABLE, GROUPS_TABLE,KEYS_TABLE 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

我需要得到all accounts details哪个是 acct_typexxcount 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 

我需要的输出应该是这样的 在此处输入图像描述

任何人请帮我解决这个问题

4

5 回答 5

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
于 2013-06-03T12:10:52.297 回答
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
于 2013-06-03T11:55:07.263 回答
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

于 2013-06-03T12:06:24.773 回答
0

出于以下几个原因,我对您的代码进行了一些调整(见下文):

  1. 总是以某种方式编写我的 SELECT 语句是有帮助的(无论如何对我来说) - 将未分组的任何内容放在首位,理想情况下将事物按照与我的 JOIN 相同的顺序并在我的 GROUP BY 中执行相同的操作
  2. 我将任何限制我的 FROM 表的东西放入 WHERE 而不是 JOIN 中,以使我想要做的事情更清楚,也便于以后修改。
  3. 我还想确保它的布局合理,以便更容易扫描问题。

接受这个重新排列的查询并通读它,以确保你得到了你所期望的行为。

PS 我不确定你的表名和引用风格 - 我通常使用反引号 (`) 并且永远不会在我的表名中添加点 (.)。如果您将它们作为占位符放入,那很好,但如果它们是真实的,它们可能会给您带来麻烦。

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
于 2013-06-03T12:10:09.797 回答
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 Fiddle 验证:http ://www.sqlfiddle.com/#!2/f4b6a/20

于 2013-06-03T12:18:50.773 回答