【问题标题】:join 3 tables and select count加入 3 个表并选择计数
【发布时间】:2019-01-04 06:04:17
【问题描述】:

我有 3 个表格 {websites} {accounts} {ads}

网站表格

id     title    url           etc
-----------------------------------
1      site1    site1.com     ...
2      site2    site1.com     ...
3      site3    site3.com     ...

帐户表

id     websiteID   username    etc
-----------------------------------
1      1           username1   ...
2      2           username2   ...
3      1           username1   ...
4      3           username5   ...

广告表

id     accountID   title    etc
---------------------------------
1      1           title1   ...
2      2           title1   ...
3      1           title3   ...
5      4           title4   ...

我想加入这三个表,从网站表开始,从网站表中获取一些数据,accounts_count与其网站相关,ads_count与其帐户相关。我也想要计数的零或空结果。 帐户表中的用户名不是唯一的,可以相同。 ads 表中的标题也不是唯一的,可以相同。

这是我的查询,但有时它会返回错误的计数结果!

SELECT 
    websites.id as website_id
,   websites.title as website_title
,   COUNT(accounts.websiteID) as accounts_count
,   COUNT(ads.accountID) as ads_count
,   ads.lastUpdate
,   websites.activation as website_activation 
FROM websites 
LEFT JOIN accounts 
    ON websites.id = accounts.websiteID 
LEFT JOIN ads 
    ON accounts.id = ads.accountID
GROUP BY websites.id;

你能帮帮我吗:{

我想在这样的表格中显示这个结果:

website_title     accounts_count   ads_count    last update  operations
-------------------------------------------------------------------------
website1           3               8            2017/07/27   etc...
website2           0               0            2017/07/27   etc...
website3           3               9            2017/07/27   etc...
website4           5               15           2017/07/27   etc...

【问题讨论】:

  • 想要的输出
  • 从预期结果中添加一个样本
  • 看着你的选择我不知道你是否真的知道你想要什么。如果您的选择包含ads.lastUpdate,那么您真正需要计算并在结果中包含什么?显示所需的输出,您尝试实现
  • 我添加了我想要实现的输出
  • 能否请您检查一下我的答案,我在您的查询中发现您错过了 group by 中的一些字段,当您应用聚合函数时,您应该考虑除未聚合的列之外的所有列

标签: mysql sql database-design count jointable


【解决方案1】:

问题是,当您使用帐户加入 websites 时,您会在 accounts 中获得每一行的行。然后,当您加入 ads 时,您会进一步增加行数。我的猜测是你现在得到相同的 accounts_countads_count 计数。此外,您还有来自ads 表的lastUpdate 列和聚合函数。

【讨论】:

    【解决方案2】:
    SELECT websites.id as website_id, websites.title as website_title,  
       ads.lastUpdate, websites.activation as website_activation 
       COUNT(accounts.websiteID) as accounts_count, COUNT(ads.accountID) as ads_count, 
    FROM websites, accounts, ads
    WHERE websites.id = accounts.websiteID 
       AND accounts.id = ads.accountID;
    

    【讨论】:

      【解决方案3】:

      似乎需要更改计数。
      ads.lastUpdate 的 MAX 会更准确。

      F.e.

      SELECT 
          websites.id as website_id
      ,   websites.title as website_title
      ,   COUNT(DISTINCT accounts.ID) as accounts_count
      ,   COUNT(ads.ID) as ads_count
      ,   MAX(ads.lastUpdate) as LastUpdateAds
      ,   websites.activation as website_activation 
      FROM websites 
      LEFT JOIN accounts 
          ON websites.id = accounts.websiteID 
      LEFT JOIN ads 
          ON accounts.id = ads.accountID
      GROUP BY websites.id;
      

      【讨论】:

      • @AlirezaKafi 请注意,lastUpdate 在此版本中也与 MAX 聚合。 count(distinct value) 计算不为空的唯一值。
      猜你喜欢
      • 2011-07-06
      • 2011-02-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多