我已经对此进行了多次搜索,但遗憾的是由于我的查询类型,它总是给我PHP答案。
我将恢复代码来生成脚本,但我想的更多,我知道必须在单个查询中以更简单的方式执行它会更快。
我的代码:
SELECT DISTINCT
se.mailingID AS "mailingID",
(
SELECT
SUM(l.averageReadTime)
FROM
eventlog AS l
INNER JOIN sends as s ON l.mailingIDKey=s.id
WHERE
s.TFPIDSanitised=se.TFPIDSanitised
AND
l.averagerEadtIme!="-1"
AND
l.averageReadTime!="(none)"
) AS "Total",
(
SELECT
SUM(l.averageReadTime)
FROM
eventlog AS l
INNER JOIN recipients AS r ON l.recipientKey=r.id
INNER JOIN devices AS d ON r.commonDeviceKey=d.id
INNER JOIN sends AS s ON l.mailingIDKey=s.id
WHERE
d.name="AOL Mail"
AND
s.mailingID=se.mailingID
AND
l.averageReadTime!="-1"
AND
l.averageReadTime!="(none)"
) AS "AOL Mail",
...
[repeat for all email app types]
...
(
SELECT
SUM(l.averageReadTime)
FROM
eventlog AS l
INNER JOIN recipients AS r ON l.recipientKey=r.id
INNER JOIN devices AS d ON r.commonDeviceKey=d.id
INNER JOIN sends AS s ON l.mailingIDKey=s.id
WHERE
d.name="AOL Desktop"
AND
s.mailingID=se.mailingID
AND
l.averageReadTime!="-1"
AND
l.averageReadTime!="(none)"
) AS "AOL Desktop"
FROM sends AS se
WHERE
(
se.sendDateTime BETWEEN '2018-01-01 00:00:00' AND '2018-01-15 23:59:59'
);
正如你所看到的,它是一个查询的怪物。
我已经在考虑删除其中一个内连接(设备名称,只需在devices.id上搜索,而不需要第三次连接)。
我所追求的是在查询中执行某种循环:
所以它就像(我不知道真正的代码)
SELECT LOOP (devices)
where device.id=recipients.commonDeviceKey
AS device.name
对于mysql来说完全是这样的吗?我正在循环中进行循环(具有讽刺意味的是,这并没有丢失在我身上)因为mysql和循环没有提出相关的答案。
答案 0 :(得分:0)
我会想象这样的事情会起作用。我没有你的确切架构,但这应该得到你需要的。
SELECT s.mailingID, SUM(l.averageReadTime), 'Total' as name
FROM eventlog AS l
JOIN sends as s ON l.mailingIDKey=s.id
WHERE
l.averagerEadtIme!="-1"
AND l.averageReadTime!="(none)"
AND s.sendDateTime BETWEEN '2018-01-01 00:00:00' AND '2018-01-15 23:59:59'
UNION ALL
SELECT s.mailingID, SUM(l.averageReadTime), d.name
FROM eventlog AS l
JOIN recipients AS r ON l.recipientKey=r.id
JOIN devices AS d ON r.commonDeviceKey=d.id
JOIN sends AS s ON l.mailingIDKey=s.id
WHERE
l.averageReadTime!="-1"
AND l.averageReadTime!="(none)"
AND s.sendDateTime BETWEEN '2018-01-01 00:00:00' AND '2018-01-15 23:59:59'
GROUP BY d.name
;
答案 1 :(得分:0)
我希望找到一种更灵活的方法,最后我将在循环中使用数据库动态创建“devicesList”但下面的工作。
感谢@Napoli的帮助,虽然你可能没有向我提供你让我思考的答案,不同的解决方案(每个设备使用线性表的两步过程)。
void