Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用多案例语句优化查询_Mysql_Join_Case - Fatal编程技术网

Mysql 如何使用多案例语句优化查询

Mysql 如何使用多案例语句优化查询,mysql,join,case,Mysql,Join,Case,我有一个查询,返回一个4列的表,其中显示了购买的数量和购买类型的子类别。问题是,由于它非常慢,我是否可以降低复杂性以提高精化的速度 select db.start as dateb, sum(case when(select case when db.preacq = db2.last and cl.prod_status_id in (1, 2, 3, 4) then dba.buy_datetime else null end as buy_date from ( select * f

我有一个查询,返回一个4列的表,其中显示了购买的数量和购买类型的子类别。问题是,由于它非常慢,我是否可以降低复杂性以提高精化的速度

select
db.start as dateb,
sum(case when(select 
case when  db.preacq = db2.last  and cl.prod_status_id in (1, 2, 3, 4) then dba.buy_datetime
else null end as buy_date
from (
select * from dbda.prod_app
) as db3 
left join (select prod_id, max(preacq) as last from  dbda.prod_app where dbda.prod_app.type = 1
group by 1 ) as db2 on db2.prod_id = db3.prod_id
order by db.preacq  desc limit 1 ) is not null then 1 else 0 end) as prod_bought ,


sum(case when(select 
case when  db.preacq = db2.last  and cl.prod_status_id in (1, 2, 3, 4) and date(db2.last)=
date(dba.buy_datetime) then dba.buy_datetime else null end as buy_date
from (
select * from dbda.prod_app
) as db3 
left join (select prod_id, max(preacq) as last from  dbda.prod_app where dbda.prod_app.type = 1
group by 1 ) as db2 on db2.prod_id = db3.prod_id
order by db.preacq  desc limit 1 ) is not null then 1 else 0 end) as prod_bought_1 , 

sum(case when(select 
case when  db.preacq = db2.last  and cl.prod_status_id in (1, 2, 3, 4) and db2.last <
dba.buy_deal_datetime and date(dba.buy_deal_datetime) < date(dba.buy_datetime) then
dba.buy_datetime else null end as buy_date
from (
select * from dbda.prod_app
) as db3 
left join (select prod_id, max(preacq) as last from  dbda.prod_app where dbda.prod_app.type = 1
group by 1 ) as db2 on db2.prod_id = db3.prod_id
order by db.preacq  desc limit 1 ) is not null then 1 else 0 end) as prod_bought_2 , 

sum(case when(select 
case when  db.preacq = db2.last  and cl.prod_status_id in (1, 2, 3, 4) and date(db2.last) <
date(dba.buy_datetime) and dba.buy_deal_datetime is null then dba.buy_datetime else null end as
buy_date
from (
select * from dbda.prod_app
) as db3 
left join (select prod_id, max(preacq) as last from  dbda.prod_app where dbda.prod_app.type = 1
group by 1 ) as db2 on db2.prod_id = db3.prod_id
order by db.preacq  desc limit 1 ) is not null then 1 else 0 end) as prod_bought_3

from dbda.prod_prod as cl
left join dbda.prod_app as db on cl.id = db.prod_id
left join dbda.prod_acq as dba  on cl.id = dba.id
group by 1
结果如下表所示

+------------+-------------+---------------+---------------+--------------+ | dateb | prod_bought | prod_bought_1 | prod_bought_2 | prod_bought_3| +------------+-------------+---------------+---------------+--------------+ | 11-11-2020 |100 | 50 | 0 | 50 | | 22-11-2020 |200 | 100 | 100 | 0 | | 23-11-2020 |300 | 200 | 50 | 50 | | 24-11-2020 |400 | 200 | 100 | 100 | +------------+-------------+---------------+---------------+--------------+
此查询不创建表,它只是返回一个行集,该行集在客户端界面中看起来像一个表。请发布任务本身:源表创建表脚本可能会简化-没有不必要的列和索引,一些示例数据(如插入到脚本中)以及此数据的所需输出(带有逻辑解释)。并指定精确的MySQL版本。