mysql - 如何在下面的例子中使用外连接?

标签 mysql sql postgresql

我正在创建 2 个查询:

1

select count(c.id) as cid1, p.name as pname1, u.name as uname1 
from crm_lead c, account_period p, res_users u 
where c.create_date between p.date_start and p.date_stop and 
      (c.user_id = u.id or c.sales_vertical=u.id) and 
      u.id = 1 
group by p.name, u.name

2

select count(c.id) as cid2, p.name as pname2, u.name as uname2 
from crm_lead c, account_period p, res_users u 
where c.create_date between p.date_start and p.date_stop and
      (c.user_id = u.id or c.sales_vertical=u.id) and 
      stage_id =12 and 
      u.id = 1 
where a.pname = b.pname 
group by p.name, u.name

对于上述 2 个查询,我得到以下结果

第一季度的结果:

 cid1         pname1      uname1                                       
 11           07/2011     admin                                        
 5            08/2011     admin                                        
 9            09/2011     admin

第二季度的结果:

 cid2        pname2    uname2                                          
 9           07/2011   admin                                           
 3           09/2011   admin

结合这两个查询后,我得到以下输出:

 cid1  cid2  pname1    pname2     uname1     uname2                    
 11     9     07/2011   07/2011   admin      admin                     
 9      3     09/2011   09/2011   admin      admin

但我想要的结果是

 cid1   cid2  pname1    pname2    uname1     uname2                    
 11     9     07/2011   07/2011   admin      admin                     
 5      0     08/2011   08/2011   admin      admin                     
 9      3     09/2011   09/2011   admin      admin

如何实现?

我的组合查询如下:

select a.cid1, b.cid2, a.pname1, b.pname2, a.uname1, b.uname2 
from (select count(c.id) as cid1, p.name as pname1, u.name1 as uname 
      from crm_lead c, account_period p, res_users u 
      where c.create_date between p.date_start and p.date_stop and 
            (c.user_id = u.id or c.sales_vertical=u.id) and 
            u.id = 1 
      group by p.name, u.name) as a,
     (select count(c.id) as cid2, p.name as pname2, u.name as uname2 
      from crm_lead c, account_period p, res_users u 
      where c.create_date between p.date_start and p.date_stop and
            (c.user_id = u.id or c.sales_vertical=u.id) and 
            stage_id =12 and 
            u.id = 1 
      group by p.name, u.name)as b 
where a.pname1 = b.pname2

最佳答案

要获得您请求的结果,查询可能如下所示:

SELECT a.cid1
      ,COALESCE(b.cid2, 0) AS cid2
      ,a.pname1
      ,COALESCE(b.pname2, a.pname2) AS pname2
      ,a.uname1
      ,COALESCE(b.uname2, a.uname2) AS uname2
FROM  (<query1>)  a
LEFT   JOIN (<query2>) b ON a.pname1 = b.pname2

但是,我怀疑您是否真的想要那个确切的结果。

关于mysql - 如何在下面的例子中使用外连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9785824/

相关文章:

mysql - XML 数据插入 MySql DB

mysql - QT mysql打不开数据库

sql - 使用内部SELECT查询“清理”任意SQL?

.net - javascript 和 SQL 之间的共享规则

node.js - 如何使用带有 Node.js 的 pg 正确查询 Postgresql 数据库?

php - 用PHP和mysql动态建表

mysql 使用 where 子句计算另一个表中的行

mysql - 如何更改 select 语句中的列值

java - 使用 Hibernate 在 PostgreSQL 中从 JSONArray 获取对象的问题

postgresql - 在 Heroku 上将 Postgres 与 Sails.js 结合使用时出错