本文介绍了如何通过与两个列进行比较从一个表中选择一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果从一个表中获得月份固定值,如果有月份和月份,如果有这样的表结构,如何找到结果。
planid |百分比| frommonths | tomonths 11 5 12 24 11 6 25 60如果我有几个月的价值,如果我有几个月的值,那么如何查找从月份到月的结果
选择 来自 表 其中 planid = 11 和个月值谎言 12-25 或 25-60,根据 固定月份值解决方案
选择 * 来自 表 其中 14 ( yourmonth) frommonths 和 tomonths
你可以将你的月份作为变量传递 例如。
声明 @ yourmonth int set @ yourmonth = 14 选择 * 来自 table 其中 @ yourmonth frommonths 和 tomonths
尝试像这样 DECLARE @ TEMPTBL 表( ID INT IDENTITY ( 1 , 1 ),FROMDMNTHS INT ,TOMONTHS INT ,PERCENTAGE INT ) INSERT INTO @TEMPTBL (FROMDMNTHS,TOMONTHS,PERCENTAGE) SELECT 2 , 25 , 5 UNION ALL SELECT 20 , 30 , 15 UNION ALL SELECT 12 , 40 , 25 UNION 所有 SELECT 5 , 50 , 35 UNION ALL SELECT 8 , 60 , 45 SELECT * FROM @ TEMPTBL SELECT * FROM @ TEMPTBL WHERE FROMDMNTHS BETWEEN 2 AND 5 和 TOMONTHS BETWEEN 5 AND 50
how to find the result if have months fixed value from one table by comparing with from months and to months if have table structure like this.
planid| percentage | frommonths | tomonths 11 5 12 24 11 6 25 60how to find result for between frommonths and to months if i have months value like 14
select from table where planid = 11 and months values lies between 12-25 or 25-60 according to fixed months values 解决方案 select * from table where 14(yourmonth) between frommonths and tomonthsyou can pass yourmonth as variable also eg.
declare @yourmonth int set @yourmonth =14 select * from table where @yourmonth between frommonths and tomonthsTRY LIKE THIS DECLARE @TEMPTBL TABLE( ID INT IDENTITY(1,1) ,FROMDMNTHS INT ,TOMONTHS INT ,PERCENTAGE INT ) INSERT INTO @TEMPTBL (FROMDMNTHS,TOMONTHS,PERCENTAGE ) SELECT 2,25,5 UNION ALL SELECT 20,30,15 UNION ALL SELECT 12,40,25 UNION ALL SELECT 5,50,35 UNION ALL SELECT 8,60,45 SELECT * FROM @TEMPTBL SELECT * FROM @TEMPTBL WHERE FROMDMNTHS BETWEEN 2 AND 5 AND TOMONTHS BETWEEN 5 AND 50更多推荐
如何通过与两个列进行比较从一个表中选择一个值
发布评论