create table teacher
(
    tid    varchar(12)           not null,
    tname  varchar(20)           not null,
    constraint PK_TEACHER primary key (tid)
)
gocreate table student
(
    xh     varchar(20)           not null,
    tid    varchar(12)           null    ,
    sname  varchar(20)           not null,
    constraint PK_STUDENT primary key (xh)
)
go
create index Relation_11_FK on student (tid)
goalter table student
    add constraint FK_STUDENT_RELATION__TEACHER foreign key  (tid)
       references teacher (tid)
go
如上,我有两个表,teacher和student.我想用left join查出一个老师下有多少个学生.
如:select teacher.* , student.* from teacher left join student on teacher.tid = student.tid;
但是我想再加上一个条件在后面,如是id为'0001'的老师所带的学生,应该怎么加?
我直接在后面加上..from teacher left join student on teacher.tid = student.tid and teacher.tid = '001'没有任何效果, 它还是会将所有老师的全查出来??

解决方案 »

  1.   

    select teacher.* , student.* from teacher left join student on teacher.tid = student.tid
     where teacher.tid = '001' 
      

  2.   

    用where。。
    select teacher.* , student.* from teacher left join student on teacher.tid = student.tid
    where teacher.tid = '001'
      

  3.   

    select teacher.* , student.* from teacher left join student on teacher.tid = student.tid where  teacher.tid = '001'
      

  4.   

    select teacher.* , student.* from teacher left join student on teacher.tid = student.tid
     where teacher.tid = '0001'另外,还有你说是“0001”,怎么条件上写的是‘001’呢,
      

  5.   

    "我直接在后面加上..from teacher left join student on teacher.tid = student.tid and teacher.tid = '001'没有任何效果, 它还是会将所有老师的全查出来??"
    在on 后边and 添加条件是做连接时用,
    此处你想限制老师的tid应该加在where 后边
    即:select teacher.* , student.* from teacher left join student on teacher.tid = student.tid where  teacher.tid = '001'