Mysql SQL多对多
我这里有五个表格: 使用者 受试者idMysql SQL多对多,mysql,sql,join,many-to-many,Mysql,Sql,Join,Many To Many,我这里有五个表格: 使用者 受试者id +------------+--------------+ | subject_id | subject_name | +------------+--------------+ | 1 | mathematics | | 2 | science | | 3 | english | +------------+--------------+ 批处理 主题组 +----------
+------------+--------------+
| subject_id | subject_name |
+------------+--------------+
| 1 | mathematics |
| 2 | science |
| 3 | english |
+------------+--------------+
批处理
主题组
+----------+------------+
| batch_id | subject_id |
+----------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| | |
+----------+------------+
科目评估
+--------+----------+
| stu_id | batch_id |
+--------+----------+
| 1 | 1 |
+--------+----------+
我试图实现的选择是
+--------+----------+------------+--------------+
| stu_id | batch_id | subject_id | subject_name |
+--------+----------+------------+--------------+
| 1 | 1 | 1 | mathematics |
| 1 | 1 | 2 | science |
| 1 | 1 | 3 | english |
| | | | |
+--------+----------+------------+--------------+
我试过了=
$query = "SELECT subject.subject_name, user.first_name, batch.batch_name
from subject_assessment inner join user
ON subject_assessment.student_id = user.user_id inner
join batch ON subject_assessment.batch_id = batch.batch_id
left join subject_batch on = subject.subject_name = subject_batch.subject_id";
但是失败了。当我移除
subject.subject_name
及
但这样我就无法检索主题id和主题名称
感谢您的帮助。提前谢谢您。以下是它的外观:
select user.stu_id, user.batch_id, subject.subject_id, subject.subject_name
from user
inner join subject_batch on user.batch_id = subject_batch.batch_id
inner join subject on subject.subject_id = subject_batch.subject_id
order by user.stu_id;
这里是您可以运行它的地方:
请改进表格的格式,现在无法查看数据。抱歉,给您带来麻烦谢谢,我能够进行编码并获取特定数据。非常感谢你。
subject.subject_name
left join left join subject_batch on = subject.subject_name = subject_batch.subject_id"
select user.stu_id, user.batch_id, subject.subject_id, subject.subject_name
from user
inner join subject_batch on user.batch_id = subject_batch.batch_id
inner join subject on subject.subject_id = subject_batch.subject_id
order by user.stu_id;