MySQL更新字段基于来自其他表的条件求和

时间:2015-11-19 00:21:57

标签: mysql join sql-update

我正在尝试使用遗留系统中的值来填充考勤系统。 我目前正在跟踪所有人的出勤率(这很简单),今年的出勤率,以及自上次评分(考试)以来的出席情况。为了避免潜在的污染,我正在存储遗留数据,并且希望一次将它导入我的总数中。

这是我的基本数据模型:

K-means

对于相关成员(初始加载),我需要将core_historic attendance中所有值的总和添加到core_member表中的year_last_grad列,其中年份大于最后一次评分的年份。

我从这篇帖子(mysql update column with value from another table)收集到,我必须做一些像

这样的事情
-- information about the attendee
CREATE TABLE core_member (
    id INT UNSIGNED AUTO_INCREMENT, 
    name VARCHAR (250), 
    dob DATE,
    last_grade_date date default '2014-12-14',
    grade INT UNSIGNED NOT NULL DEFAULT 50,
    PRIMARY KEY (id)
);

-- attendee data 
INSERT INTO core_member values(1, 'Donald Duck', '1950-03-01', '2015-06-15', 10);
INSERT INTO core_member values(2, 'Goofy', '1950-04-17', '2014-06-15', 42);


-- attendance sums (1:1 with antendee)
CREATE TABLE core_attendance(
    medlemsid INT UNSIGNED,
    imported INT DEFAULT 0,
    all_time INT DEFAULT 0,
    since_last_grad INT DEFAULT 0,
    UNIQUE KEY medlemsid (medlemsid),
    FOREIGN KEY (medlemsid) REFERENCES core_member (id)
);

-- attendance sum data
INSERT INTO core_attendance values(1,100,150,0);
INSERT INTO core_attendance values(2,80,103,3);

-- historic attendance
CREATE TABLE core_historic_attendance (
  medlemsid int(10) unsigned NOT NULL,
  year int(11) NOT NULL,
  month enum('none','januar','februar','marts','april','maj','juni','juli','august','september','oktober','november','december') NOT NULL DEFAULT 'none',
  attendance int(11) DEFAULT '0',
  UNIQUE KEY unq (medlemsid,year,month),
  KEY medlemsid_idx (medlemsid),
  CONSTRAINT medlemsid FOREIGN KEY (medlemsid) REFERENCES core_member (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);

-- historic attendance data
INSERT INTO core_historic_attendance values(1,2011,'none',54);
INSERT INTO core_historic_attendance values(1,2012,'none',43);
INSERT INTO core_historic_attendance values(1,2013,'none',61);
INSERT INTO core_historic_attendance values(1,2014,'none',49);
INSERT INTO core_historic_attendance values(1,2015,'januar',19);
INSERT INTO core_historic_attendance values(1,2015,'februar',14);
INSERT INTO core_historic_attendance values(1,2015,'marts',17);
INSERT INTO core_historic_attendance values(2,2011,'none',54);
INSERT INTO core_historic_attendance values(2,2012,'none',43);
INSERT INTO core_historic_attendance values(2,2013,'none',61);
INSERT INTO core_historic_attendance values(2,2014,'none',49);
INSERT INTO core_historic_attendance values(2,2015,'januar',19);
INSERT INTO core_historic_attendance values(2,2015,'februar',4);
INSERT INTO core_historic_attendance values(2,2015,'marts',7);

但是我有点卡住,因为我只想要年份大于UPDATE core_members INNER JOIN core_historic_attendance ON id = core_historic_attendance.medlemsid having`year` > year(last_grad_date) SET since_last_grad = since_last_grad + SUM(attendance); 的core_historic_attendance记录的总和

编辑:

我已按照Sasha的建议纠正了SQL,我收到错误代码1111“无效使用群组功能”

我也创建了SQL Fiddle

1 个答案:

答案 0 :(得分:1)

这适合你吗?

CREATE TEMPORARY TABLE t1 (key(medlemsid)) SELECT core_historic_attendance.medlemsid,SUM(attendance) as total_attendance
FROM core_member INNER JOIN core_historic_attendance ON
 id = core_historic_attendance.medlemsid AND `year` > year(last_grade_date)
GROUP BY medlemsid;

UPDATE core_attendance INNER JOIN t1  USING(medlemsid)
SET since_last_grad = since_last_grad +  t1.total_attendance;