共有三个表,
学生表student
studentid
studentname老师表teacher
teacherid
teachedname统计表count
counttype  '记录为老师或学生
countid    '对应的老师或学生的id求一基于count表的sql,有countid,countname 两项
如果是counttyype='老师' 则countid从teacher中取 countname
如果是counttype='学生'  则countid从student中取 countname

解决方案 »

  1.   

    select m.countid , countname = n.teachedname from [count] m , teacher n where m.countid = n.teacherid and m.counttype = '老师'
    union all
    select m.countid , countname = n.studentname from [count] m , student n where m.countid = n.studentid and m.counttype = '学生'
      

  2.   


    select '老师' as counttype,teachedname as countid
    from teacher
    union all
    select '学生',studentname
    from student