鉴于这两个表
subject subject_changes
----------- ------------------
id id
subjectName version
subjectCode subjectCode
version previousSubjectCode
如何在subjectCode中显示所有更改?我必须根据previousSubjectCode引用subjectCode并获取该版本的相关数据。
在主题表中,我有
1, 'Animals','Q001', 1
2, 'Animal Kingdom', 'Q012',2
2, 'Animal Kingdom', 'Q023',3
和subject_changes
1, 2, 'Q012', 'Q001'
2, 3, 'Q023', 'Q012'
在伪代码中
SELECT * FROM subject WHERE subjectCode= ?
SELECT * FROM subject_changes WHERE subjectCode=?
foreach subjectCode
get previousSubjectCode
get subjectName from subject table
SELECT * FROM subject WHERE subjectCode = subject_changes.previousSubjectCode
结果表应该看起来像
version subjectName subjectCode
------- ----------- ----------
version 3 Animal Kingdom Q023
version 2 Animal Kingdom Q012
version 1 Animal Q001
答案 0 :(得分:0)
SELECT
subject.version, subject.subjectName, subject.subjectCode
FROM subject
INNER JOIN subject_changes USING (subjectCode)
WHERE subjectCode = ?