SQLite3 select field by numeric range

2019-08-07 01:21发布

问题:

in SQLite3, say i have this simple table

rowid     value1    value2
(int)     (float)   (float)  
1         15.3      20.2
2         17.8      30.5
3         15.8      25.3
4         16.1      48.0

How could I select those rows where the values are:
15.0 >= value1 <= 16.5 - effectively selecting rows 1,2 & 3

further how can i refine adding to the first criteria this to ALSO 20.0 >= value2 <= 37.0 - selecting then only row 1 & 3?

Your help is highly appreciated

回答1:

I believe your first logic is not correct, for if value1 is less than 15, it will also be less than 16.5, but just change the operators:

Select * from SimpleTable where value1 <= 15.0 and value1 <= 16.5;

Maybe you meant:

Select * from SimpleTable where value1 >= 15.0 and value1 <= 16.5;

Again (considering the adjusted logic):

Select * from SimpleTable where value1 >= 15.0 and value1 <= 16.5 and value2 >= 20.0 and value2 <= 37.0;

You might want to add and "order by" clause if you want your results sorted.



回答2:

Simply execute the SQL command below :

select id, value1, value2 FROM <table name> WHERE value1 >= 15.0 AND  value1 <= 16.5 AND value2 >= 20.0 AND value2 <= 37.0