3个表的SQL查询

来源:百度知道 编辑:UC知道 时间:2024/06/24 15:19:30
我这里有3个表 t1 t2 t3 用t1的一个字段查出主键 然后用该主键查询t2表 能查出多个结果 但该结果是t3表的主键 然后查出T3表中不包含t2表查出信息的所有信息
例如 t1表为用户表 主键为t_id 还有其他比如姓名 年龄等 t2为课程表 主键为t_id 还有课程名等 t3表为用户和课程关系表 主键为t_id 还有2个外键为user_id 和lesson_id user_id的值就是用户表t_id的值 lesson_id的值就是课程表t2的t_id值
现在就是要求就是 用课程表中的课程名查出没选这门课的用户所有信息 然后5个5个的显示出来! 比如显示要10-15条的信息

select *
from t1
where t_id not in (select user_id
from t3
where lesson_id=(select t_id
from t2
where 课程名='课程名'))

5个5个的显示出是不是就显示5个

select top 5 *
from t1
where t_id not in (select user_id
from t3
where lesson_id=(select t_id
from t2
where 课程名='课程名'))

这个主要是用外连接
假设T4是T1,T2连接后的结果表

select t3.*
from t3 left join t4 on t3.id=t4.id
where t4.id is null

问题不清楚!!!!!!!!!