有表ZK数量 折扣
2     10
4     15
6     20当用户输入1时,就不带出折扣,当用户输入2时就带出折扣10.也就是说满足数量条件就带出相应的折扣但是这个数量条件和折扣都是是不定的,经常会去变,所以我想把数量和折扣也做成变量,求高手指点

解决方案 »

  1.   

    if object_id('[ZK]') is not null drop table [ZK]
    go
    create table [ZK] (数量 int,折扣 int)
    insert into [ZK]
    select 2,10 union all
    select 4,15 union all
    select 6,20select * from [ZK]
    declare @i int 
    set @i = 2select 数量,折扣 from ZK where 数量 = @i/*
    数量 折扣
    2 10*/
    LZ说的都变成变量是?
      

  2.   

    你这个不对,当@i = 1 或者3、5和大于6的时候折扣不就没了么, 但实际我要的是当@i 为3时,折扣仍旧是10,数量2那一档;
    @i为5时,折扣是15,数量4那一档,
    @i大于等于6时,折扣就是20.
      

  3.   

    declare @i int 
    set @i=3
    if @i<2 
       select 1[数量],0[折扣]
    else if @i<4
      select @i [数量],折扣 from zk where 数量=2
    else if @i<6
      select @i [数量],折扣 from zk where 数量=4
    else 
      select @i [数量],折扣 from zk where 数量=6
    结果:数量 结果
    3    10看看是不是你想要的?
      

  4.   


    declare @qty int,@discount int
    select @qty=1;
    select @discount=isnull((select top 1 折扣 from 你的表
    where t.数量<=@qty order by 数量 desc),0);
    select @qty 数量, @discount 折扣;