MySQL结合了3个选择查询的结果

时间:2014-04-08 16:19:19

标签: mysql sql

我在MySQL中遇到一个所谓的简单选择查询时出现问题,这里的情况如下: 我有3张桌子'学生' PresenceRecords1'和' PresenceRecords2'以下数据

*学生

╔════╦══════╦═══════════╦══════════╗
║ id ║ sex  ║ firstname ║ lastname ║
╠════╬══════╬═══════════╬══════════╣
║  1 ║ masc ║ john      ║ doe      ║
║  2 ║ fem  ║ lisa      ║ simpson  ║
╚════╩══════╩═══════════╩══════════╝

* PresenceRecords1

╔════╦════════════╦════════════╦════════╗
║ id ║ 1student_id║    date    ║ points ║
╠════╬════════════╬════════════╬════════╣
║  1 ║          1 ║ 2014-01-01 ║      1 ║
║  2 ║          2 ║ 2014-01-01 ║      1 ║
║  3 ║          1 ║ 2014-01-02 ║      5 ║
║  4 ║          2 ║ 2014-01-03 ║      1 ║
╚════╩════════════╩════════════╩════════╝

* PresenceRecords2

╔════╦════════════╦════════════╦════════╗
║ id ║ student_id ║    date    ║ points ║
╠════╬════════════╬════════════╬════════╣
║  1 ║          1 ║ 2013-01-01 ║     10 ║
║  2 ║          1 ║ 2011-02-01 ║      1 ║
║  3 ║          1 ║ 2014-02-02 ║      5 ║
║  4 ║          2 ║ 2014-03-02 ║     15 ║
╚════╩════════════╩════════════╩════════╝

我应该查询表并获得以下结果:

╔═══════════════════╦══════════════════╦═══════╦══════════════╗
║ student_firstname ║ student_lastname ║ score ║ lastpresence ║
╠═══════════════════╬══════════════════╬═══════╬══════════════╣
║ john              ║ doe              ║    22 ║ 2014-02-02   ║
║ lisa              ║ simpson          ║    17 ║ 2014-03-02   ║
╚═══════════════════╩══════════════════╩═══════╩══════════════╝

这是我使用的查询:

SELECT 
    s.first_name as student_firstname,
    s.last_name as student_lastname,
    SUM(pr1.score + pr2.score) as prpoints,
    MAX(pr1.date) as maxdatepr1,
    MAX(pr2.date) as maxdatepr2
FROM
    student AS s
        INNER JOIN
    PresenceRecords1 AS pr1 ON s.id = pr1.1student_id
        INNER JOIN
    PresenceRecords2 AS pr2 ON pr1.1student_id = pr2.student_id
group by s.id

我得到了这个结果:

╔═══════════════════╦══════════════════╦══════════╦════════════════════════════════╦═════════════════════════════════╗
║ STUDENT_FIRSTNAME ║ STUDENT_LASTNAME ║ PRPOINTS ║           MAXDATEPR1           ║           MAXDATEPR2            ║
╠═══════════════════╬══════════════════╬══════════╬════════════════════════════════╬═════════════════════════════════╣
║ john              ║ doe              ║       50 ║ January, 02 2014 00:00:00+0000 ║ February, 02 2014 00:00:00+0000 ║
║ lisa              ║ simpson          ║       32 ║ January, 03 2014 00:00:00+0000 ║ March, 02 2014 00:00:00+0000    ║
╚═══════════════════╩══════════════════╩══════════╩════════════════════════════════╩═════════════════════════════════╝

有人能指出我正确的方向吗?

修改 很抱歉这篇长篇文章here is a SQLFIDDLE of my problem

2 个答案:

答案 0 :(得分:1)

您可以使用UNION,但如果您可以将数据存储在一个表中以显示状态记录而不是将其保存在单个表中,我会要求您更改架构

SELECT 
    s.first_name as student_firstname,
    s.last_name as student_lastname,
    SUM(pr.score ) as prpoints,
    MAX(pr.date) as maxdatepr1    
 FROM   student AS s
JOIN 
(
SELECT 1student_id sid,score,date FROM PresenceRecords1
UNION 
SELECT student_id sid,score,date FROM PresenceRecords2
) pr ON(pr.sid =s.id)

group by s.id

Fiddle Demo

答案 1 :(得分:0)

SELECT s.id,s.sex,s.first_name,s.last_name, SUM(x.score),MAX(x.date) FROM student s
JOIN
 (SELECT * FROM presencerecords2
 UNION
 SELECT * FROM presencerecords1
 ) x
ON x.student_id = s.id
GROUP BY s.id;