【问题标题】:Joining 2 SQL SELECT result into one query将 2 个 SQL SELECT 结果加入一个查询
【发布时间】:2021-03-02 23:01:54
【问题描述】:

我想知道是否有办法将两个或多个结果集合并为一个。 我有以下两个查询 第一个查询:

SELECT
  CONCAT(day(db.prod_id.created_on),"-",month(db.prod_id.created_on),"-",year(db.prod_id.created_on)) as day_month_year,
  db.country.country ,
  count(concat(day(db.prod_id.created_on),"-",month(db.prod_id.created_on),"-",year(db.prod_id.created_on))) as count ,
  COUNT(DISTINCT db.prod_id.email) AS MAIL
from db.prod_id
left join db.country on db.prod_id.branch_id = db.country.id
where  db.prod_id.created_on > '2020-11-17'  and (db.country.type = 1 or db.country.type = 2)
group by
  concat(day(db.prod_id.created_on),"-",month(db.prod_id.created_on),"-",year(db.prod_id.created_on)),
  db.country.country
order by db.prod_id.created_on

第二个查询:

select
  CONCAT(day(db.prod_id.created_on),"-",month(db.prod_id.created_on),"-",year(db.prod_id.created_on)) as day_month_year,
  db.country.country,
  count(CONCAT(day(db.prod_id.created_on),"-",month(db.prod_id.created_on),"-",year(db.prod_id.created_on))) as count_BUY
from db.prod_id
left join db.prod_evaluations on db.prod_id.id = db.prod_evaluations.id
left join db.country on db.prod_id.branch_id = db.country.id
left join (Select prod_properties.prod_id, prod_properties.value From prod_properties Where prod_properties.property_id = 5) as db3 on db3.prod_id = db.prod_id.id
where db.prod_id.created_on > '2020-11-17'
and db3.value  = 'online-buy' and db.prod_id.status_id <> 25
group by
  concat(day(db.prod_id.created_on),"-",month(db.prod_id.created_on),"-",year(db.prod_id.created_on)),
  db.country.country
order by db.prod_id.created_on

第一个查询给出以下结果:

+------------+---------+--------+------+ |天 |国家 |计数 |邮件 | +------------+---------+--------+------+ | 17-11-2020 |资讯科技 | 200 | 100 | | 17-11-2020 |美国 | 250 | 100 | | 18-11-2020 |资讯科技 | 350 | 300 | | 18-11-2020 |美国 | 200 | 100 | +------------+---------+--------+------+

第二个查询给出:

+------------+---------+------------+ |天 |国家 |计数_购买 | +------------+---------+------------+ | 17-11-2020 |资讯科技 | 50 | | 17-11-2020 |美国 | 70 | | 18-11-2020 |资讯科技 | 200 | | 18-11-2020 |美国 | 50 | +------------+---------+------------+

现在我想将这两个结果合并为一个:

+------------+---------+--------+------+----------- + |天 |国家 |计数 |邮件 |计数_购买 | +------------+---------+--------+------+----------- + | 17-11-2020 |资讯科技 | 200 | 100 | 50 | | 17-11-2020 |美国 | 250 | 100 | 70 | | 18-11-2020 |资讯科技 | 350 | 300 | 200 | | 18-11-2020 |美国 | 200 | 100 | 50 | +------------+---------+--------+------+----------- +

如何执行此查询? 我正在使用 mysql 谢谢

【问题讨论】:

  • 欢迎来到 S/O。请编辑您的问题,发布您拥有的示例结构和示例数据。然后展示你想要得到的东西。同时显示您尝试过的 SQL。
  • 请提供样本数据和所需结果以及适当的数据库标签。您当前的查询也很有用。
  • 您已经知道您刚刚使用国家列使用的连接
  • created_on 是日期还是日期时间?
  • 这是一个日期时间,但我需要它的“DAY”粒度。

标签: mysql sql join subquery


【解决方案1】:

简单的方法:你可以加入查询。

select *
from ( <your first query here> ) first_query
join ( <your second query here> ) second_query using (day_month_year, country)
order by day_month_year, country;

这是一个内部连接。当然,您也可以外连接。不过,MySQL 不支持 full 外连接。如果你想这样做,你必须查看如何在 MySQL 中模拟完全外部连接。

困难的方式 ;-) 合并查询。

如果我没记错的话,你的两个查询可以简化为

select
  date(created_on),
  branch_id as country,
  count(*) as count_products,
  count(distinct p.email) as count_emails
from db.prod_id
where created_on >= date '2020-11-17'
and branch_id  in (select country from db.country where type in (1, 2))
group by date(created_on), branch_id
order by date(created_on), branch_id;

select
  date(created_on),
  branch_id as country,
  count(*) as count_buy
from db.prod_id
where created_on >= date '2020-11-17'
and status_id <> 25
and prod_id in (select prod_id from prod_properties where property_id = 5 and status_id <> 25)
group by date(created_on), branch_id
order by date(created_on), branch_id;

两者结合应该是

select
  date(created_on),
  branch_id as country,
  sum(branch_id in (select country from db.country where type in (1, 2)) as count_products,
  count(distinct case when branch_id  in (select country from db.country where type in (1, 2) then p.email end) as count_emails,
  sum(status_id <> 25 and prod_id in (select prod_id from prod_properties where property_id = 5 and status_id <> 25)) as count_buy
from db.prod_id
where created_on >= date '2020-11-17'
group by date(created_on), branch_id
order by date(created_on), branch_id;

您会看到,查询的共同条件保留在 where 子句中,而其他条件则放在聚合函数中。

sum(boolean)sum(case when boolean then 1 else 0 end) 的缩写,即计算 MySQL 中满足条件的行数。

【讨论】:

  • 感谢它的工作,我需要检查数据是否正确。困难的方式是什么?
  • 困难的方法是将两个查询合二为一。这可以通过条件聚合来完成。我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 2014-04-01
  • 1970-01-01
  • 2018-06-08
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多