gpt4 book ai didi

mysql - 在不同的月份和总值上加入同一个表中的两个查询

转载 作者:行者123 更新时间:2023-11-30 22:32:46 25 4
gpt4 key购买 nike

我想得到一个表,该表的总和为 MONTH(NOW())member_total_point 列和 MONTH(NOW() INTERVAL 1 MONTH) 的列。如何选择表格?

编辑

我不应该在 select 上使用 [..]

这是我的新查询:

SELECT la.member_created, la.member_api_domain, la.member_api_campaign, 
la.total, lb.total_now
FROM (SELECT member_created, member_api_domain, member_api_campaign, SUM(member_total_point) AS 'total'
FROM lalights_member WHERE YEAR(member_created)=YEAR(NOW())
AND MONTH(member_created)= MONTH(NOW()-INTERVAL 1 MONTH)
GROUP BY member_api_campaign ) AS la
LEFT JOIN (SELECT member_created,SUM(member_total_point)
AS 'total_now'
FROM lalights_member WHERE YEAR(member_created)=YEAR(NOW())
AND MONTH(member_created)= MONTH(NOW())
ORDER BY member_api_campaign DESC) AS lb
ON la.member_created = lb.member_created

但是在 SQLyog 上出现这个错误:

'[total], lb.[total_now] FROM (Select member_created, member_api_domain, member_a' at line 1

我正在使用 MySQL 5.1..

示例数据:

member_created       member_api_domain  member_api_campaign member_total_point
-------------- ----------------- ------------------- ------------------
2015-09-22 18:30:20 domain campaign 4 (september)
2015-09-22 18:30:20 domain campaign 7 (september)
2015-08-22 18:30:20 domain campaign 2 (august)
2015-08-22 18:30:20 domain campaign 7 (august)
2015-09-22 18:30:20 asdf jujuju 4 (september)
2015-08-22 18:30:20 asdf jujuju 9 (august)

示例输出:

member_api_campaign this_month last_1month last_2month
------------------- ---------- ----------- -----------
domain campaign 11(september) 9 (august)
asdf jujuju 4 (september) 9 (august)

提醒一下,现在是 10 月

最佳答案

  • 更改单引号(均值字符串)作为旁引号或仅删除字段别名的引号

    'total'     for `total`
    'total_now' for `total_now`
  • 删除 ORDER BY member_api_campaign DESC

.

SELECT la.member_created
,la.member_api_domain
,la.member_api_campaign
,la.total
,lb.total_now
FROM (
SELECT member_created
,member_api_domain
,member_api_campaign
,SUM(member_total_point) AS 'total'
FROM lalights_member
WHERE YEAR(member_created) = YEAR(NOW())
AND MONTH(member_created) = MONTH(NOW() - INTERVAL 1 MONTH)
GROUP BY member_api_campaign
) AS la
LEFT JOIN (
SELECT member_created
,SUM(member_total_point) AS 'total_now'
FROM lalights_member
WHERE YEAR(member_created) = YEAR(NOW())
AND MONTH(member_created) = MONTH(NOW())
) AS lb ON la.member_created = lb.member_created

关于mysql - 在不同的月份和总值上加入同一个表中的两个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com