当前位置: 代码迷 >> Sql Server >> 又是查询!该如何解决
  详细解决方案

又是查询!该如何解决

热度:83   发布时间:2016-04-27 19:25:24.0
又是查询!
SQL code
create table ta(月份 int,编码 int,工资 int)insert ta select 1,2113,900union all select 2,2113,780create table tb(编码 int,月份 int,奖金 int)insert tb select 2113,1,200union all select 2113,2,100怎么得到月份    编码    工资    奖金 1     2113   900    200 2     2113   780    100我写的不行.select a.*,奖金=case when a.月份=(select b.月份 from ta b where b.编码=a.编码 and b.月份=a.月份) then 奖金 else 0 end from ta a join tb b on a.编码=b.编码


------解决方案--------------------
SQL code
create table ta(月份 int,编码 int,工资 int)insert ta select 1,2113,900union all select 2,2113,780create table tb(编码 int,月份 int,奖金 int)insert tb select 2113,1,200union all select 2113,2,100select ta.*, tb.奖金 from ta,tb where ta.月份 = tb.月份 and ta.编码 = tb.编码/*月份          编码          工资          奖金          ----------- ----------- ----------- ----------- 1           2113        900         2002           2113        780         100(所影响的行数为 2 行)*/drop table ta,tb
代码迷推荐解决方案:软件开发者薪资,http://www.daimami.com/other/1391128.html
  相关解决方案