[英]Joining 2 SQL SELECT result into one query
我想知道是否有办法将两个或多个结果集合并为一个。 我有以下两个查询 第一个查询:
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
第一个查询给出以下结果:
+------------+---------+-------+------+ | day | Country | Count | Mail | +------------+---------+-------+------+ | 17-11-2020 | IT | 200 | 100 | | 17-11-2020 | US | 250 | 100 | | 18-11-2020 | IT | 350 | 300 | | 18-11-2020 | US | 200 | 100 | +------------+---------+-------+------+
第二个查询给出:
+------------+---------+-----------+ | day | Country | Count_BUY | +------------+---------+-----------+ | 17-11-2020 | IT | 50 | | 17-11-2020 | US | 70 | | 18-11-2020 | IT | 200 | | 18-11-2020 | US | 50 | +------------+---------+-----------+
现在我想将这两个结果合二为一:
+------------+---------+-------+------+-----------+ | day | Country | Count | Mail | Count_BUY | +------------+---------+-------+------+-----------+ | 17-11-2020 | IT | 200 | 100 | 50 | | 17-11-2020 | US | 250 | 100 | 70 | | 18-11-2020 | IT | 350 | 300 | 200 | | 18-11-2020 | US | 200 | 100 | 50 | +------------+---------+-------+------+-----------+
如何执行此查询? 我正在使用 mysql 谢谢
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 不支持完全外部联接。 如果需要,则必须查找如何在 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 中满足条件的行数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.