我有2个相同的表(_impressionsdaily,_impressionsalltime),在每天结束时,我将所有数据从每天移动到所有时间。 结构是:ID,购买ID,印象,唯一,日期。
我运行以下查询:
SELECT
daily.purchaseID,
SUM(daily.impression + IFNULL(alltime.impression, 0)) AS impression,
SUM(daily.unique + IFNULL(alltime.unique, 0)) AS `unique`
FROM _impressionsdaily daily
LEFT JOIN _impressionsalltime alltime ON alltime.purchaseID= daily.purchaseID
GROUP BY daily.purchaseID;
问题是,如果所有时间表中的购买都没有在日常生活中找到,我将不会看到它们。
基本上,我们的想法是每天和所有时间的所有印象总和。
--- EDIT 事情发生在每天凌晨12:00,每日表被复制到alltime然后被删除。 所以给出的解决方案是错误的..
感谢您的帮助, 丹尼
答案 0 :(得分:1)
我认为你正在寻找一个Union Select。尝试这样的事情:
SELECT DISTINCT(p.ID), p.purchaseID
FROM (SELECT *
FROM _impressionsdaily
UNION SELECT *
FROM _impressionsalltime) AS p
GROUP BY p.purchaseID
答案 1 :(得分:0)
SELECT
daily.purchaseID,
SUM(daily.impression + IFNULL(alltime.impression, 0)) AS impression,
SUM(daily.unique + IFNULL(alltime.unique, 0)) AS `unique`
FROM _impressionsdaily daily
LEFT JOIN _impressionsalltime alltime ON alltime.purchaseID= daily.purchaseID
GROUP BY daily.purchaseID;
在这种情况下,您尚未选择任何字段形式_impressionsalltime表。
您可以使用此sql从_impressionsalltime表中选择字段
SELECT
alltime.*,
daily.purchaseID,
SUM(daily.impression + IFNULL(alltime.impression, 0)) AS impression,
SUM(daily.unique + IFNULL(alltime.unique, 0)) AS `unique`
FROM _impressionsdaily daily
LEFT JOIN _impressionsalltime alltime ON alltime.purchaseID= daily.purchaseID
GROUP BY daily.purchaseID;