题意明确:
所有来自浙江大学的用户题目回答明细情况
问题分解:
- 限定条件:来自浙江大学的用户,学校信息在用户画像表,答题情况在用户练习明细表,因此需要通过device_id关联两个表的数据; 方法1:join两个表,用inner join,条件是
on up.device_id=qpd.device_id and up.university='浙江大学'
方法2:先从画像表找到浙江大学的所有学生id列表where university='浙江大学'
,再去练习明细表筛选出id在这个列表的记录,用where in
完整代码:
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | select qpd.device_id, qpd.question_id, qpd.result
from question_practice_detail as qpd
inner join user_profile as up
on up.device_id=qpd.device_id and up.university=``'浙江大学'
order by question_id
|
方法2代码:
|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | select device_id, question_id, result
from question_practice_detail
where device_id in (
``select device_id from user_profile
``where university=``'浙江大学'
)
order by question_id
|