I have these two tables that relate to each other with the column itemid
. I managed to make the sum of different itemid
values as I show in the first example from data_table
.
Now I want to relate this sum by specific dates in the other table but I can not. Please can anyone help me?
items_table
+--------+-----------+------------+ | itemid | published | publish_up | +--------+-----------+------------+ | 1 | 1 | 02-01-2014 | +--------+-----------+------------+
data_table
+--------+---------+------+ | itemid | fieldid | data | +--------+---------+------+ | 1 | 27 | 5 | | 1 | 28 | 10 | | 1 | 29 | 5 | +--------+---------+------+
This query is ok:
SELECT (SELECT SUM(data) FROM data_table WHERE fieldid='27')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='28')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='29')
But not between dates:
SELECT (SELECT SUM(data) FROM data_table WHERE fieldid='27')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='28')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='29')
WHERE (publish_up BETWEEN '2014-01-01 00:00:00' AND '2014-01-31 00:00:00')
FROM items_table
SELECT itemid, SUM(data)
FROM data_table JOIN items_table USING (itemid)
WHERE publish_up >= '2014-01-01'
AND publish_up < '2014-02-01'
GROUP BY itemid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.