我有一个小问题。我开发了一个订单管理系统。作为一项新功能,用户现在应该能够在列表视图中查看每个订单的一些状态。
我的表是这样的:
订单:
id INT AUTO PRIMARY,
orderNo VARCHAR(20),
customerId INT,
salesmanId INT,
orderdate DATE
订单状态:
id INT AUTO PRIMARY,
type ENUM (1,2,3,4),
orderId INT,
plannedDate DATE,
finalDate DATE,
isApproved ENUM(0,1)
我当前列出数据的查询是:
SELECT
o.orderNo,
cst.name as customerName,
u.name as salesmanName,
o.orderdate,
t1.plannedDate as t1Planned,
t2.plannedDate as t2Planned,
t3.plannedDate as t3Planned,
t4.plannedDate as t4Planned,
t1.finalDate as t1Final,
t2.finalDate as t2Final,
t3.finalDate as t3Final,
t4.finalDate as t4Final,
t1.isApproved as t1App,
t2.isApproved as t2App,
t3.isApproved as t3App,
t4.isApproved as t4App
FROM orders as o
LEFT JOIN customers as cst ON ( cst.id = o.customerId)
LEFT JOIN users as u ON ( u.id = o.salesmanId )
LEFT JOIN orderStatus as t1 ON ( t1.type = 1 AND t1.orderId = o.id )
LEFT JOIN orderStatus as t2 ON ( t2.type = 2 AND t2.orderId = o.id )
LEFT JOIN orderStatus as t3 ON ( t3.type = 3 AND t3.orderId = o.id )
LEFT JOIN orderStatus as t4 ON ( t4.type = 4 AND t4.orderId = o.id )
ORDER BY
o.orderNo DESC
如您所见 - 很多左连接,我希望可以以不同的方式完成。主要问题是这个查询非常慢。我希望有人可以帮助我。提前致谢。