当前位置: 代码迷 >> Sql Server >> 帮看一下这样的sql语句怎样写解决思路
  详细解决方案

帮看一下这样的sql语句怎样写解决思路

热度:71   发布时间:2016-04-27 12:25:22.0
帮看一下这样的sql语句怎样写
teacherid department professional classname coursename  
111111 信息工程系 计算机应用技术 09-1 VB 理论 2011-2012第二学期 
111111 信息工程系 计算机应用技术 09-1 Java 理论 2011-2012第二学期
222222 信息工程系 计算机应用技术 09-1 动态网页制作 理论 2011-2012第二学期
222222 信息工程系 计算机应用技术 09-1 动态网页制作 实践 2011-2012第二学期
111111 信息工程系 计算机应用技术 09-1 离散数学 理论 2010-2011第一学期 
111111 信息工程系 计算机应用技术 10-1 VB 理论 2011-2012第二学期 
想根据teacherid选择出department、professional、classname,但不重复
比如:teacherid=111111
得到结果:department professional classname  
  信息工程系 计算机应用技术 09-1
  信息工程系 计算机应用技术 10-1
请问这样的Sql语句怎样写,高手指点

------解决方案--------------------
SQL code
--方法1  with tb1 as(    select '111111' as teacherid,'信息工程系' as department,'计算机应用技术' as professional,'09-1' as classname,'VB 理论 2011-2012第二学期' as coursename union all    select '111111','信息工程系','计算机应用技术','09-1','Java 理论 2011-2012第二学期' union all    select '222222','信息工程系','计算机应用技术','09-1','动态网页制作 理论 2011-2012第二学期' union all    select '222222','信息工程系','计算机应用技术','09-1','动态网页制作 实践 2011-2012第二学期' union all    select '111111','信息工程系','计算机应用技术','09-1','离散数学 理论 2010-2011第一学期' union all    select '111111','信息工程系','计算机应用技术','10-1','VB 理论 2011-2012第二学期')select distinct    department,    professional,    classname from tb1    where teacherid='111111'/*department professional   classname---------- -------------- ---------信息工程系      计算机应用技术        09-1信息工程系      计算机应用技术        10-1(2 row(s) affected)*/go--方法2;with tb2 as(    select '111111' as teacherid,'信息工程系' as department,'计算机应用技术' as professional,'09-1' as classname,'VB 理论 2011-2012第二学期' as coursename union all    select '111111','信息工程系','计算机应用技术','09-1','Java 理论 2011-2012第二学期' union all    select '222222','信息工程系','计算机应用技术','09-1','动态网页制作 理论 2011-2012第二学期' union all    select '222222','信息工程系','计算机应用技术','09-1','动态网页制作 实践 2011-2012第二学期' union all    select '111111','信息工程系','计算机应用技术','09-1','离散数学 理论 2010-2011第一学期' union all    select '111111','信息工程系','计算机应用技术','10-1','VB 理论 2011-2012第二学期')select     department,    professional,    classname from tb2    where teacherid='111111'group by department,    professional,    classname/*department professional   classname---------- -------------- ---------信息工程系      计算机应用技术        09-1信息工程系      计算机应用技术        10-1(2 row(s) affected)*/
  相关解决方案