繁体   English   中英

SQL查询插入并获取长度范围内的价格

[英]SQL Query to insert and get price within a range to length

可以说我的长度范围有不同的价格

Length      Price
0 - 10        18
11 - 15       20
16 - 20       30
21 - 40       45
41 - infinity 60

假设我输入的长度为10.625 ,如何将这些信息存储在数据库中以及如何检索这些信息? 如何获得长度为10.625的商品价格?


我不确定我是否已经解决了这个问题

priceData.Length_End = Math.Ceiling(priceData.Length);

        string selectStatement = 
            "SELECT Price PriceList2 " +
            "WHERE @Length_End >= Length_Start AND @Length <= Length_End";

然后我得到了第一个读者的价值

SqlDataReader reader = selectCommand.ExecuteReader();
while (reader.Read())
{
   price = decimal.Parse(reader["Price_Per_Ton"].ToString());
   break;
}
reader.Close();

如果我错了,请纠正我。 感谢您的所有回复!

好吧 ,我相信您只需要存储Length即可,当您想在查询中获取Price列时,可以使用BETWEEN 喜欢;

Select Price
From YourTable
Where Length BETWEEN 11 AND 15

这是SQL Fiddle Demo

但是仅当您知道每个范围的最大和最小Length值时,此结构才可以。 如果还要将它们存储在数据库中,则应将“ Length列分隔为两个不同的列。 喜欢;

Max_Length | Min_Length | Price
0          | 10         | 18
11         | 15         | 20 
16         | 20         | 30
21         | 40         | 45
41         |            | 60

Select Price
From YourTable
Where Min_Length < 10.625 AND Max_Length > 10.625

您可以将结构更改为:

len_start    len_end      price
0            10           18
11           15           20
16           20           30
21           40           45
41           infinity     60

并运行查询,如:

declare @len decimal
set @len= 10.615

select price from table
Where len_start <= @len and @len < len_end

如果要使用NULL作为无穷大,请运行以下查询:

select price from table
Where (len_end is not null AND len_start <= @len and @len < len_end) OR 
      (len_end is null AND len_start <= @len)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM