Sql 将具有不同where子句的两个查询合并到一个表中

Sql 将具有不同where子句的两个查询合并到一个表中,sql,Sql,这可能是一个非常简单的问题,但我有两个单独的查询,我想将它们合并到一个表中。其中一个查询显示每月登录我的网站,另一个查询显示已购买的注册。我想将两个查询合并到一个表中,因此我有下表: Month Sign-ups Sign-ups with Purchase Jan 2019 250 40 Feb 2019 500 120 我该怎么做?以下是我的两个问题: 注册: select count(u.id) as Sign_Ups , month(

这可能是一个非常简单的问题,但我有两个单独的查询,我想将它们合并到一个表中。其中一个查询显示每月登录我的网站,另一个查询显示已购买的注册。我想将两个查询合并到一个表中,因此我有下表:

Month      Sign-ups  Sign-ups with Purchase
Jan 2019   250       40
Feb 2019   500       120
我该怎么做?以下是我的两个问题:

注册:

select
  count(u.id) as Sign_Ups
  , month(From_iso8601_timestamp(u.created)) as Month
from
   prodjoinreel.users u
where
  year(From_iso8601_timestamp(u.created)) = 2019
group by
  2
order by
  2 asc
注册购买:

select
  count(distinct g.owner) as Sign_Ups_with_Reel
  , month(From_iso8601_timestamp(u.created)) as Month
from
  prodjoinreel.goals g
  right join prodjoinreel.users u on
    g.owner = u.id
where
  year(From_iso8601_timestamp(u.created)) = 2019
group by
  2
order by
  2 asc

谢谢大家!

我认为您可以在第二个查询中添加一个
计数(不同的u.id)

select
  month(From_iso8601_timestamp(u.created)) as Month,
  count(distinct u.id) as Sign_Ups,
  count(distinct g.owner) as Sign_Ups_with_Reel
from
  prodjoinreel.users u
  left join prodjoinreel.goals g on g.owner = u.id
where year(From_iso8601_timestamp(u.created)) = 2019
group by 1
order by 1

注意:我将您的
右连接
更改为
左连接
;大多数人倾向于认为<代码>右连接< /代码> s是相当反直觉的,并且赞成“代码>左连接< /代码> s。”p> 您需要按1分组,谢谢!有没有办法添加另一列,其中包含一个特定的where子句,该子句不适用于“登录”列和“使用卷盘”列?@LanePortman:欢迎!我不确定你在评论中的确切意思。我只能回答你提出的问题。如果您认为它正确地回答了您的问题,请单击复选标记。如果您还有其他问题,我建议您问一个新问题。您使用的是哪种产品?“SQL”只是一种查询语言,而不是特定数据库产品的名称。请为您正在使用的数据库产品添加。跳过GROUP BY和ORDER BY中不推荐使用的序号,改为指定列名(或别名)。塔尔将使查询具有可移植性和向前兼容性。