简体   繁体   中英

How to use between and greater than in sql

table name:tax
       slab1    slab2    tax
        0        10000   0
        10001    50000   10
        50001    100000  20
        100001    0      30

Here 0 represents infinity.

my table is like this.I have to find the tax of 60000.I dont't know how to use between and greater than in sql together.

I tried like this:
$query="SELECT * FROM taxsettings 
        where $liable2 between tax.slab1 and tax.slab2 
           or $liable2 > tax.slab1";

$liable2 is the amount. any one can say how insert infinity value to db table

Try this

SELECT * FROM taxsettings 
WHERE 60000 (between slab1  and  slab2) or 60000>slab2    

try this query

"SELECT * FROM taxsettings 
        where $liable2 between case when tax.slab1=0 then $liable2 else tax.slab1 end  and case when tax.slab2 =0 then $liable2 else tax.slab2 end;"

Try;

   SELECT * 
    FROM taxsettings 
    WHERE  $liable2 >= slab1  and  
    $liable2 <= 
    CASE when slab2 = 0 then 1000000000000 --some big number a upper limit
    else slab2 end

Get the largest slab1 value thats smaller than the value you're checking ($liable2) :

SELECT Tax
FROM taxsettings
WHERE slab1 <= $liable2
ORDER By slab1 desc
Limit 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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