-3

我为 3 种不同的活动准备了 3 张不同的桌子。公共字段是 user_id。

表现:

id | user_id | date  | mark    
1  | 123     |  xx   |  20
2  | 456     |  xx   |  10
3  | 789     |  xx   |  5
4  | 123     |  xx   |  10
5  | 456     |  xx   |  10
6  | 789     |  xx   |  5

内部活动:

id | user_id | date  | mark    
1  | 123     |  xx   |  20
2  | 456     |  xx   |  10
3  | 789     |  xx   |  5
4  | 123     |  xx   |  10
5  | 456     |  xx   |  10
6  | 789     |  xx   |  5

其他活动:

id | user_id | date  | mark    
1  | 123     |  xx   |  20
2  | 456     |  xx   |  10
3  | 789     |  xx   |  5
4  | 123     |  xx   |  10
5  | 456     |  xx   |  10
6  | 789     |  xx   |  5

如何获取所有 3 个表中的每个用户标记并在 DESC 中对其进行排序。结果应该像

id | user_id |  mark    
1  | 123     |   90
2  | 456     |   60
3  | 789     |   30

提前致谢

4

3 回答 3

2
SELECT tmp.ID,tmp.USER_ID,tmp.SUM(mark) 
FROM 
(select * from Performance

union all

select * from Internal_Activities

union all 

select * from Other_Activities
) as tmp
group by tmp.USER_ID

注意:-为什么要保留 3 个具有相同架构的单独表,你不能让它成为一个吗?

于 2013-11-11T07:23:16.300 回答
1

Try this:

SELECT p.id, p.user_id, SUM(p.mark) FROM Performance p 
LEFT JOIN Internal Activities ia ON p.id=ia.id
LEFT JOIN Other Activities oa ON ia.id=oa.id 
GROUP BY p.user_id; 
于 2013-11-11T07:54:40.497 回答
0
SELECT 
 p.id
,p.user_id
,@performance:=p.mark AS performance
,@other_activities:=oa.mark AS other_activities
,@internal_activities:=ia.mark AS internal_activities
,@total_mark:=@performance+@other_activities+@internal_activities AS total_mark
FROM
table1 p
LEFT JOIN table2 ia
USING(user_id)
LEFT JOIN table2 oa
USING(user_id)
于 2013-11-11T07:30:10.763 回答