我有两张桌子
users
student_id username
1 Alex
2 Tom
3 Max
answers
exercise_done student_id
4 1
5 1
6 2
在“用户”表中,它列出了具有相应ID的用户名。我想做的是在php中显示表“ answers”中的所有数据,但在“ student_id”列中,我想显示用户名。我知道如何显示“答案”表,但我想在“ student_id”列中显示用户名。要显示表格“答案”,这是我所拥有的:
SELECT exercise_done, student_id from answers
我们称这为联合:
SELECT A.exercise_done, U.student_id, U.username
FROM answers AS A
JOIN users AS U
ON U.student_id = A.student_id
这就是为什么在关系模型中,您需要唯一的表中具有外键的原因。有了他们,你可以做这样的关节。
有多种关节类型,但这对您有好处。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句