如何通过与两个列进行比较从一个表中选择一个值 [英] how to select one value from one table by comparing with two its columns

查看:59
本文介绍了如何通过与两个列进行比较从一个表中选择一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



 planid |百分比| frommonths | tomonths 
11 5 12 24
11 6 < span class =code-digit> 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


这篇关于如何通过与两个列进行比较从一个表中选择一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆