我应该使用哪个sql连接?

ef1yzkbh  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(226)

我有两张table,我需要把它们合起来。
这是我的table和我需要的东西

vuktfyat

vuktfyat1#

可能是这样的:

SELECT c.Data, c.Group, c.Count AS 'Users Count', a.Count AS 'Application Count'  
   FROM clients c 
   LEFT JOIN applications a
     ON c.Data=a.DATA
     AND c.Group=a.Group

你可以在这里测试。

biswetbf

biswetbf2#

这将是一个 left join ,以将clients表中的所有行以及applications表中的可用行带出:

select 
    c.date,
    c.group,
    c.count client_count,
    coalesce(a.count, 0) application_count
from clients-by_days c
left join applications_by_days a
    on a.date = c.date
    and a.group = c.group

如果两个表中都缺少匹配项,则需要 full join :

select 
    date,
    group,
    coalesce(c.count, 0)client_count,
    coalesce(a.count, 0) application_count
from clients-by_days c
left join applications_by_days a
    using(date, group)

不是所有的数据库支持 full join (并非所有人都支持 using() )连接 predicate 的语法)。

相关问题