我有三张这样的表:
学生
stuNum | stuName
------------------
2012 | jack
2013 | tom
测验
quizNum | quizName
------------------
1 | chapter 1
2 | chapter 2
3 | chapter 3
studentassessment
stuNum | quizNum | assessmentMark
-----------------------------------
2012 | 1 | 10
2012 | 2 | 8
2012 | 3 | 10
2013 | 1 | 5
我希望得到类似这样的结果
stuNum | stuName | Quiz Num | assessmentMark
--------------------------------------------
2012 | jack | 1 | 10
2012 | jack | 2 | 8
2012 | jack | 3 | 10
描述:所有三个表都已连接..我想得到所有stuNum已经完成的stuNum = 2012,quizNum。
我尝试了几种组合来获取结果,但无法正常工作。 这是我尝试的示例组合:
$sql = "select a.stuNum,a.quizNum,a.assessmentMark from studentassessment a inner join student b on a.stuNum=b.stuNum inner join quiz c on a.quizNum=c.quizNum where b.stuNum='2012'"
答案 0 :(得分:0)
<强>查询强>
SELECT a.stuNum,
b.stuName,
a.quizNum,
c.quizName,
a.assessmentMark
FROM studentAssessment a
JOIN student b
ON a.stuNum=b.stuNum
JOIN quiz c
ON a.quizNum=c.quizNum
WHERE a.stuNum=2012;
<强>输出强>
+--------+---------+---------+-----------+----------------+
| STUNUM | STUNAME | QUIZNUM | QUIZNAME | ASSESSMENTMARK |
+--------+---------+---------+-----------+----------------+
| 2012 | Jack | 1 | chapter 1 | 10 |
| 2012 | Jack | 2 | chapter 2 | 8 |
| 2012 | Jack | 3 | chapter 3 | 10 |
+--------+---------+---------+-----------+----------------+
<强> Fiddle demo 强>
答案 1 :(得分:0)
在这里试试吧
SELECT a.stuNum,b.stuName, a.quizNum, a.assessmentMark
FROM studentassessment a
INNER JOIN student b ON a.stuNum = b.stuNum
WHERE b.stuNum='2012'
您不需要在测验表上放置内部联接,因为您既不从中检索任何值,也不使用它来推断输出中的某些关系