如何通过与两个列进行比较从一个表中选择一个值

编程入门 行业动态 更新时间:2024-10-24 10:16:22
本文介绍了如何通过与两个列进行比较从一个表中选择一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果从一个表中获得月份固定值,如果有月份和月份,如果有这样的表结构,如何找到结果。

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 60

how 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 tomonths

you can pass yourmonth as variable also eg.

declare @yourmonth int set @yourmonth =14 select * from table where @yourmonth between frommonths and tomonths

TRY 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

更多推荐

如何通过与两个列进行比较从一个表中选择一个值

本文发布于:2023-10-26 09:46:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!