2

我有一个小问题。我开发了一个订单管理系统。作为一项新功能,用户现在应该能够在列表视图中查看每个订单的一些状态。

我的表是这样的:

订单:

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

如您所见 - 很多左连接,我希望可以以不同的方式完成。主要问题是这个查询非常慢。我希望有人可以帮助我。提前致谢。

4

1 回答 1

1

更新:您可以使用条件聚合

SELECT o.orderNo,
       c.name as customerName,
       u.name as salesmanName,
       o.orderdate,
       MAX(CASE WHEN s.type = 1 THEN plannedDate END) Planned1,
       MAX(CASE WHEN s.type = 2 THEN plannedDate END) Planned2,
       MAX(CASE WHEN s.type = 3 THEN plannedDate END) Planned3,
       MAX(CASE WHEN s.type = 4 THEN plannedDate END) Planned4,
       MAX(CASE WHEN s.type = 1 THEN finalDate   END) Final1,
       MAX(CASE WHEN s.type = 2 THEN finalDate   END) Final2,
       MAX(CASE WHEN s.type = 3 THEN finalDate   END) Final3,
       MAX(CASE WHEN s.type = 4 THEN finalDate   END) Final4,
       MAX(CASE WHEN s.type = 1 THEN isApproved  END) App1,
       MAX(CASE WHEN s.type = 2 THEN isApproved  END) App2,
       MAX(CASE WHEN s.type = 3 THEN isApproved  END) App3,
       MAX(CASE WHEN s.type = 4 THEN isApproved  END) App4
  FROM salesorderStatus s JOIN salesorders o
    ON s.orderId = o.id JOIN customers c
    ON o.customerId = c.id JOIN users u 
    ON o.salesmanId = u.id
 GROUP BY o.orderNo, c.name, u.name, o.orderdate
 ORDER BY o.orderNo DESC

样本输出:

+---------+-------------+--------------+--------- ---+------------+------------+------------+------- -----+------------+------------+------------+----- --------+------+------+------+------+
| 订单号 | 客户姓名 | 业务员姓名 | 订购日期 | 计划1 | 计划2 | 计划3 | 计划4 | 决赛1 | 决赛2 | 决赛3 | 决赛4 | 应用程序1 | 应用程序2 | 应用程序3 | 应用程序4 |
+---------+-------------+--------------+--------- ---+------------+------------+------------+------- -----+------------+------------+------------+----- --------+------+------+------+------+
| E1015 | 微软 | 史蒂芬 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-15 | 2013-10-15 | 2013-10-15 | 2013-10-15 | 1 | 0 | 1 | 0 |
| E1014 | 仅 | 安德斯 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-15 | 2013-10-15 | 2013-10-15 | 2013-10-15 | 1 | 0 | 1 | 0 |
| E1013 | 专家 | 托马斯 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-01 | 2013-10-15 | 2013-10-15 | 2013-10-15 | 2013-10-15 | 1 | 0 | 1 | 0 |
...
+---------+-------------+--------------+--------- ---+------------+------------+------------+------- -----+------------+------------+------------+----- --------+------+------+------+------+

这是SQLFiddle演示

于 2013-11-05T20:11:04.787 回答