select c.id as cid, c.name as cname, s.id as sid, s.code , s.name as sname, s.gender, s.age, t.id as id, t.name as name -- from student s,teacher t,class c where s.class_id=c.id -- and t.id=(select c.teacher_id from class c where c.id=#{id}) from class c left outer join student s on c.id=s.class_id left OUTER join teacher t on t.id=c.teacher_id where c.id = 1用嵌套方式怎么写
时间: 2023-06-27 20:08:20 浏览: 177
儿童节快乐html网页.zip
以下是使用子查询的方式来嵌套查询:
```
SELECT c.id as cid, c.name as cname, s.id as sid, s.code, s.name as sname, s.gender, s.age, t.id as tid, t.name as tname
FROM (
SELECT * FROM class WHERE id = 1
) c
LEFT OUTER JOIN student s ON c.id = s.class_id
LEFT OUTER JOIN teacher t ON c.teacher_id = t.id;
```
注意,这里的子查询 `SELECT * FROM class WHERE id = 1` 返回的结果只有一行,因为我们指定了 `id = 1`,所以外层的 `LEFT OUTER JOIN` 只会返回这个班级的所有学生和对应的老师信息。
阅读全文