开发者

MySQL - getting SUM of MAX results from 2 tables

开发者 https://www.devze.com 2022-12-27 04:36 出处:网络
Here\'s my problem: I have 2 identical tables (past month data, current month data) - data_2010_03, data_2010_04:

Here's my problem: I have 2 identical tables (past month data, current month data) - data_2010_03, data_2010_04:

Content_type (VARCHAR), content_id (INT), month_count (INT), pubDate (DATETIME)

Data in month_count is updated hourly, so for each combination of content_type and content_id we insert new row开发者_如何学Go, where value of month_count is incrementally updated.

Now I try something like this:

SELECT MAX(t1.month_count) AS max_1, MAX(t2.month_count) AS max_2, SUM(max_1 + max_2) AS result, t1.content_type, t1.content_id

FROM data_2010_03 AS t1

JOIN data_2010_04 AS t2 ON t1.content_type = t2.content_type AND t1.content_id = t2.content_id

WHERE t2.pubDate < '2010-04-08' AND t1.content_type = 'video'

GROUP BY t1.content_id

ORDER BY result desc, max_1 desc, max_2 desc

LIMIT 0,10

I get an error "Unknown column 'max_1' in 'field list'. Please help.


You can not use aliases in expressions (on the same level of select) so

SUM(max_1 + max_2) AS result

has to be written as

MAX(t1.month_count)+MAX(t2.month_count) AS result


Instead of

SELECT MAX(t1.month_count) AS max_1, MAX(t2.month_count) AS max_2, SUM(max_1 + max_2) AS result

Can you just do

SELECT MAX(t1.month_count) + MAX(t2.month_count) AS result

Or I may have misunderstood the requirement.

0

精彩评论

暂无评论...
验证码 换一张
取 消