需要这个查询的逻辑?

时间:2013-07-29 06:09:43

标签: sql sql-server

我是Sql的初学者请指导我。我有两张桌子1)tblEmploee& 2)tblAgeGroup

enter image description here

enter image description here

将AgeGroupName实现到tblEmploee表的最佳方法是什么。

1 个答案:

答案 0 :(得分:6)

解决方案#1 -

SELECT e.*, a.AgeGroupName 
FROM dbo.tblEmploee e
JOIN dbo.tblAgeGroup a ON e.EmpAge BETWEEN a.AgeGroupMinAge AND a.AgeGroupMaxAge

解决方案#2 -

SELECT *
FROM dbo.tblEmploee e
OUTER APPLY (
     SELECT TOP 1 a.AgeGroupName 
     FROM dbo.tblAgeGroup a
     WHERE e.EmpAge BETWEEN a.AgeGroupMinAge AND a.AgeGroupMaxAge 
) a

解决方案#3 -

SELECT e.*, AgeGroupName = (
     SELECT a.AgeGroupName 
     FROM dbo.tblAgeGroup a
     WHERE e.EmpAge BETWEEN a.AgeGroupMinAge AND a.AgeGroupMaxAge
) 
FROM dbo.tblEmploee e