简体   繁体   English

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

[英]MySQL combine the result of 3 select queries

I'm having trouble with a supposedly simple select query in MySQL, here's the scenario: I have 3 tables 'Student', 'PresenceRecords1' and 'PresenceRecords2' with the following data, 我在使用MySQL中的一个简单的select查询时遇到了麻烦,这是这种情况:我有3个表“ Student”,“ PresenceRecords1”和“ PresenceRecords2”,其中包含以下数据,

*Student *学生

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

*PresenceRecords1 * 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 * 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 ║
╚════╩════════════╩════════════╩════════╝

I'm supposed to query the tables and got the following result: 我应该查询表并得到以下结果:

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

this is the query I'm using: 这是我正在使用的查询:

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

and I'm getting this result: 我得到这个结果:

╔═══════════════════╦══════════════════╦══════════╦════════════════════════════════╦═════════════════════════════════╗
║ 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    ║
╚═══════════════════╩══════════════════╩══════════╩════════════════════════════════╩═════════════════════════════════╝

Can someone point me in the right direction? 有人可以指出我正确的方向吗?

EDIT: Sorry for the long post, here is a SQLFIDDLE of my problem . 编辑:对不起,很长的帖子, 这是我的问题的SQLFIDDLE

You can use UNION ,but i ask you to change your schema if you can store the data in one table for presence records rather saving them in individual table 您可以使用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 小提琴演示

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM