如何加入两个表格 [英] How do I join two tables

查看:94
本文介绍了如何加入两个表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我现在正在接受挑战。



我是尝试按区域计算每次下载。我已经有了这个代码。 (

Hello everyone,

I'm having a challenge right now.

I'm trying to count each downloads by their zones. I have the code for this already. (

SELECT zone, COUNT(zone)
FROM (SELECT DISTINCT email, zone FROM downloaders)
AS result
GROUP BY zone







在另一张桌子上,我收到了管理员带来的电子邮件在下载中。



我想显示区域,下载次数(次数)和区域/管理员电子邮件。



请帮助,非常紧急。



这是代码给我一个错误如下:




)

On another table, I have the emails of the admin managers bringing in the downloads.

I would like to display the zones,number of downloads(count),and the zone/admin emails.

Please kindly help, its quite urgent.

Here is code giving me an error below:

SELECT zone_email.email, downloaders.zone, COUNT(downloaders.zone),
FROM (SELECT DISTINCT downloaders.email, downloaders.zone FROM downloaders) 
AS result
GROUP BY zone
INNER JOIN zone_email
on downloader.zone=zone_email.zone
ORDER BY zone_email.email;

推荐答案

最好的方法是将数据分组到子查询中并在连接中使用它。

The best way is to group the data in a subquery and use that in the join.
select 
  zone_email.email, 
  zone_email.zone, -- x.zone may be null, this won't be
  coalesce(x.count, 0) as 'count' -- if there is nothing, show 0
from 
  zone_email 
  left join ( -- left join to not lose data
    select zone, count(distinct email) as 'count' -- distinct emails per zone
    from downloaders group by zone
  ) x on 
    x.zone = zone_email.zone


这篇关于如何加入两个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆