【发布时间】:2019-08-14 11:45:53
【问题描述】:
我已经更改了我的 mysql 结构,并且过去使用 SUM CASE 列 keyindex 进行了此查询:
SELECT
orders.id,
orders.tool_id,
date(orders.date_placement) AS cdate,
CAST(orders.date_placement AS DATE) AS lala,
positions.id,
positions.optionindex,
positions.keyindex,
tools.id,
tools.tool_name,
tools.tool_number,
SUM(CASE WHEN keyindex=1 THEN 1 ELSE 0 END) AS value1,
SUM(CASE WHEN keyindex=2 THEN 1 ELSE 0 END) AS value2,
SUM(CASE WHEN keyindex=3 THEN 1 ELSE 0 END) AS value3,
SUM(CASE WHEN keyindex=4 THEN 1 ELSE 0 END) AS value4,
DATE_FORMAT(orders.date_placement, '%Y-%m') AS nicecdate
FROM orders
LEFT JOIN tools
ON tools.id=orders.tool_id
LEFT JOIN positions
ON positions.order_id=orders.id
WHERE
tools.id = ?
group by DATE_FORMAT(orders.date_placement, '%Y-%m')
这个查询的结果看起来:
ID | value1 | value2 | value3 | nicedate
1 | 1 | 1 | 4 | 2018-09
2 | 0 | 0 | 1 | 2018-10
3 | 1 | 1 | 1 | 2018-11
4 | 2 | 0 | 0 | 2018-12
在前端创建一些堆叠图表很好。
但是现在我添加了一个带有映射值(ID1=value1, ID2=value2, ....)的附加表(repkey),并且我在第一个表 keyindex2 中添加了一个新列除了 keyindex。
ID | keyindex1 | keyindex2 | created
1 | 1 | 27 | 2019-01
2 | 1 | 27 | 2019-01
3 | 2 | 25 | 2019-02
4 | 1 | 27 | 2019-03
5 | 10 | 27 | 2019-04
因此,我需要基于相应月份的 keyindex 和 keyindex2 的每个组合:
ID | keyindex1 | keyindex2 | Value |count |created
1 | 1 | 27 | value1 value27 | 2 |2019-01
2 | 2 | 25 | value2 value25 | 1 |2019-02
3 | 1 | 27 | value1 value27 | 1 |2019-03
4 | 10 | 27 | value10 value27 | 1 |2019-04
这是我尝试过的,但我不知道如何计算相应月份的 keyindex 和 keyindex2 的组合。结果也与旧结构不同,所以我完全不确定这个查询是否完全符合我的要求。
SELECT
orders.id,
orders.tool_id,
date(orders.date_placement) AS cdate,
CAST(orders.date_placement AS DATE) AS lala,
positions_list.id AS POSITIONID,
positions_list.order_id AS POSITIONORDERID,
positions_list.keyindex,
positions_list.keyindex2,
RepK.keynr,
RepK.content AS repcontent,
RepK.p_company,
RepK2.keynr,
RepK2.content AS repcontent2,
RepK2.p_company,
COUNT(positions_list.keyindex) AS count,
COUNT(positions_list.keyindex2) AS count2,
DATE_FORMAT(orders.date_placement, '%Y-%m') AS nicecdate
from orders
JOIN tools
ON tools.id=orders.tool_id
JOIN positions_list
ON positions_list.order_id = orders.id
JOIN repkey as RepK
ON RepK.keynr=positions_list.keyindex
AND RepK.p_company=orders.comp_id
JOIN repkey AS RepK2
ON RepK2.keynr=positions_list.keyindex2
WHERE
tools.id =:id
group by DATE_FORMAT(orders.date_placement, '%Y-%m')
顺便说一句:此查询非常慢(超过 10 秒)。网络服务器/数据库位于带有 ssd 的 nginx 上,因此问题一定出在我的查询中。 JOINS 是原因吗?
【问题讨论】:
-
不清楚你的问题是什么..是关于性能的问题??? ..或关于错误的结果.. ???你用的是哪个版本的mysql???
-
问题是关于错误的结果。 10.1.37-MariaDB-1~拉伸
-
为此,您应该更新您的问题,添加适当的(连贯的)数据样本、您的实际结果和预期结果。