问题描述
|
我有五张桌子
orders(order_id,user_id,)
order_info(id,price,tax,discount)
users(user_id,uadd_id)
user_address(uadd_id,type,city_id)
city_info(city_id,city_name,city_code)
我需要获取一份订单报告,其中包括来自order_info
表的订单信息,通常来自用户表的user_@R_852_4045@ion以及来自user_address
表的用户运输和帐单地址。 it3ѭ与shipping4ѭ对应,user_address.type
商店(如果是送货或开票地址)。因此,对于相同的订单,我需要两行user_address,一行用于装运,另一行用于计费。 “ 6”表包含城市的详细信息,应与用户地址表结合使用以获取用户地址的完整信息。
我试过这个查询
select o.*,oi.*,billing_info.name as binfo_name,shipping_info.name_name
from order o
left join order_info oi on o.order_id = oi.id
left join (
select * from user u1
left join user_address ua1 on ua1.uadd_id = u1.uadd_id
left join city_info ci1 on ua1.city_id = ci1.city_id where ua1.type = \'billing\') as billing_info
on billing_info.user_id = o.user_id
left join (select * from user u2
left join user_address ua2 on ua2.uadd_id = u2.uadd_id
left join city_info ci2 on ua2.city_id = ci2.city_id
where ua1.type = \'shipping\' ) as shipping_info
on shipping_info.user_id = o.user_id
where 1;
(我已经为billing_info和shipping_info选择了其他列)这行得通,我可以同时获得账单和运输信息,但是问题是查询需要花费很多时间(几乎2秒)来执行。我在所有表中都有大量数据。所以我需要做其他事情。
我尝试使用临时表来存储所有必需的运输和账单信息,并将订单表与这两个表一起加入,但是仍然花费大量时间(再次2秒)
这是我尝试使用临时表的方式:
create temporary table shipping_info
select * from user u1
left join user_address ua1 on ua1.uadd_id = u1.uadd_id
left join city_info ci1 on ua1.city_id = ci1.city_id
where ua1.type = \'billing\';
create temporary table billing_info
select * from user u1
left join user_address ua1 on ua1.uadd_id = u1.uadd_id
left join city_info ci1 on ua1.city_id = ci1.city_id
where ua1.type = \'shipping\';
select o.*,shipping_info.name_name
from order o
left join order_info oi on o.order_id = oi.id
left join shipping_info on shipping_info.user_id = o.user_id
left join billing_info on billing_info.user_id = o.user_id
where 1;
有人可以帮助我如何使用上述任何一种方式或其他方式有效地做到这一点吗?
如果由于语法而导致查询中出现任何错误,请暂时忽略,因为我不得不更改实际查询以缩短任何可能的红色数据,并使用此临时表与其他表联接以选择所需的数据。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)