编写查询以显示2012年在TCS中工作的用户的名称,按顺序排序。 [英] Write a query to display the names of users who have been working in TCS in the year 2012, sorted in order.
本文介绍了编写查询以显示2012年在TCS中工作的用户的名称,按顺序排序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
选择名称来自 user 其中 profile_id ( select profile_id 来自经验其中 company_name = ' TCS' 和((年( end )> = ' 2012' 和 year(start)< = ' 2012')或 end null ))订单 按名称;
解决方案
您收到错误是因为您对列和表名使用了保留字user
,结束
,名称
。
所有你需要做的就是用这些名字包围方括号即
选择 [name]
来自 [用户] 其中 profile_id in
(
选择 profile_id
来自体验< span class =code-keyword>其中 company_name = ' TCS' 和((年([结束])> = ' 2012' 和年(开始)< = ' 2012')或 [ end ] null )
) order by [name];
您对年份(结束)的检查也有点混乱 - 您只需要检查是否平等,因为您只查看datepart年...
选择 [name]
来自 [ user ] 其中 profile_id
(
选择 profile_id
来自经验其中 company_name = ' TCS' 和(年([结束])= 2012 或 [ end ] null )
)订单 按 [name];
在表体验中使用子查询的更好方法是使用INNER JOIN - 这样只有匹配两个表的数据才会回。即
选择 [name]
来自 [用户] U
INNER JOIN 体验E ON U.profile_id = E.profile_id
其中 E. company_name = ' TCS' 和(年份(E. [ end ])= 2012 或 E. [ end ] null )
订单 按 [name];
select name from user where profile_id in (select profile_id from experience where company_name='TCS' and ((year(end)>='2012' and year(start)<='2012') or end is null)) order by name;
解决方案
You are getting errors because you have used reserved words for column and table namesuser
,end
,name
.
All you need to do is surround those names with square brackets i.e.
select [name] from [user] where profile_id in ( select profile_id from experience where company_name='TCS' and ((year([end])>='2012' and year(start)<='2012') or [end] is null) ) order by [name];
Your checking of year(end) is also a little messy - you only need to check for equality because you are only looking at the datepart Year...
select [name] from [user] where profile_id in ( select profile_id from experience where company_name='TCS' and (year([end]) = 2012 or [end] is null) ) order by [name];
A better approach to using the sub-query on table experience would be to use an INNER JOIN - that way only data that matches both tables will be returned. I.e.
select [name] from [user] U INNER JOIN experience E ON U.profile_id = E.profile_id where E.company_name='TCS' and (year(E.[end]) = 2012 or E.[end] is null) order by [name];
这篇关于编写查询以显示2012年在TCS中工作的用户的名称,按顺序排序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文