列的总和返回值的两倍

时间:2015-02-04 21:46:13

标签: mysql

我有以下SQL查询:

SELECT s.name, s.learner_tag, s.guild_id, s.id, ifnull(sum(t.xp),0) as xp, ifnull(sum(a.ap),0) as ap, (ifnull(sum(t.xp),0)/1000+ifnull(sum(a.ap),0)/100) as level, g.name
    FROM student s
    INNER JOIN guild g ON g.id = s.guild_id
    LEFT JOIN student_task t ON s.id = t.student_id
    LEFT JOIN student_achievement a ON s.id = a.student_id
    WHERE s.class_id = ".$realm_id."
    GROUP BY s.name, s.learner_tag, s.guild_id, s.id, g.name
    ORDER BY level DESC LIMIT 10";

但是查询返回的是ap列的预期值的两倍。 student_achievement表中只有1条记录,值为25.但是我的查询返回50作为sum(a.ap)的值。如果我编辑该值,它仍然会加倍。 sum(t.xp)工作正常。谁能发现我的错误?

表格结构:

 CREATE TABLE student
(
    id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    name VARCHAR(64) NOT NULL,
    guild_id INT,
    class_id INT NOT NULL,
    time_updated INT,
    ap INT NOT NULL,
    learner_tag VARCHAR(64) NOT NULL,
    schoology_id INT NOT NULL,
    enrollment_id INT NOT NULL,
    FOREIGN KEY (guild_id) REFERENCES guild (id),
    FOREIGN KEY (class_id) REFERENCES class (id)
);

    CREATE TABLE student_achievement
(
    id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    student_id INT NOT NULL,
    achievement_id INT NOT NULL,
    time_added INT,
    ap INT NOT NULL,
    FOREIGN KEY (student_id) REFERENCES student (id),
    FOREIGN KEY (achievement_id) REFERENCES achievement (id)
);

    CREATE TABLE achievement
(
    id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    name VARCHAR(64) NOT NULL,
    value INT NOT NULL,
    repeatable TINYINT NOT NULL,
    image_url VARCHAR(200) NOT NULL,
    description VARCHAR(240) NOT NULL
);
CREATE TABLE task
(
    id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    name VARCHAR(64) NOT NULL,
    max_xp INT,
    schoology_id INT,
    class_id INT NOT NULL,
    processed TINYINT,
    type VARCHAR(15) NOT NULL,
    FOREIGN KEY (class_id) REFERENCES class (id)
);

0 个答案:

没有答案