如何在以下条件下为4个表编写JOIN QUERY

时间:2013-06-03 11:46:07

标签: php mysql join count left-join

我有4个表ACCOUNTS_TABLELINKS_TABLEGROUPS_TABLEKEYS_TABLE enter image description here

enter image description here

enter image description here

enter image description here

我需要all accounts details xx,其中count of Links, groups& keywordscount 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

{{1}}

我所需的输出应该是这样的 enter image description here

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

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:0)

由于以下几个原因,我重新调整了您的代码(见下文):

  1. 总是以某种方式编写我的SELECT语句(对我来说很有帮助) - 将任何未被分组的事物放在第一位,并且理想地将事物放在与我的JOIN相同的顺序中并在GROUP BY中执行相同的操作< / LI>
  2. 我把限制我的FROM表的任何东西放到WHERE而不是JOIN中,以使我更清楚我正在尝试做什么,并且以后更容易修改。
  3. 我还想确保它的布局合理,以便更轻松地扫描问题。
  4. 获取此重新排列的查询并通读它以确保您获得了您期望的行为。

    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
    

答案 4 :(得分: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