Using the MySQL command prompt, I want to SELECT all rows conataining a specific fractional part of number.
I have a field of type FLOAT called priority & I want to find all rows where the priority is X.2 where X can be any whole number.
Is there a way to use the MOD or FLOOR function to extract the ".2" in a query?
I've tried:
SELECT * from table
WHERE priority-FLOOR(priority) = .2 *(note the decimal point before the 2)*
but it returns empty set when I know there are at least 100 rows containing a priority of X.2
Thanks
What about using the LIKE clause in MySQL?
SELECT * FROM table WHERE priority LIKE %.2%
Floating point numbers are notoriously fickle to work with. That is why SQL offers the decimal data type, which is fixed length.
Do what you want with between:
SELECT *
from table
WHERE priority-FLOOR(priority) between 0.15 and 0.25
Consider storing the priority as a decimal instead of a floating point number.
If you have "deeper" priorities, like 1.22 that you are trying to avoid, then do:
WHERE priority-FLOOR(priority) between 0.195 and 0.205
The range can be even narrower, if you need.
Related
Here is a MySQL table, which has three rows.
ram cpu
1 2
4 8
8 16
if I give input as ram=7 and cpu=9,
the query has to smartly select the closest row based on the two input values and return me a single row.
example result which I expect for 7,9 combination:
ram cpu
4 8
is this possible to achieve?
Consider me having thousands of combinations like this. I need a query that always smartly returns a single row.
You never gave us an exact heuristic for what "closest" means, given that there are two columns involved. Assuming you can accept the record whose sum of absolute difference from the two input values in smallest, then here is one way:
SELECT ram, cpu
FROM yourTable
ORDER BY
ABS(ram - 7) + ABS(cpu - 9)
LIMIT 1;
Demo
I haev been stuck in a problem from quite some time and trying to figure out how to solve this using sql.
I have a table which has 3 columns :
LowerLimit UpperLimit Code
1 10 A
10.01 20 B
20.01 40 C
40.01 100 D
So in such case I need to check if there overlap present or not. The Upperlimit should not match with the LowerLimit of the next row and the permissible difference is only 0.01 . Is it possible to solve this using queries or do I need to iterate the whole range and find whether there is no overlap???
Any help is appreciated.
You can do this with exists to get the first row of overlap. For your specific logic:
select t.*
from t
where exists (select 1
from t t2
where t2.upperlimit >= t.lowerlimit and
t2.upperlimit < t.upperlimit + 0.01
);
If you want both rows, you can formulate this as a join or using a second exists to get the previous row.
I don't like your data representation. I would simple make the lower bound inclusive and the upper bound exclusive. Then the next lower bound could simply be the previous upper bound. You would not be able to use between but that is a bad idea anyway on numbers with decimal parts.
I have a table with 7.5 million entries, and while importing some of the data a few of the column breaks messed up and the first digit of a column ended up stuck onto the end of the previous column.
For example, on a row it should say ELWS=123.44 and t2=17.00, and instead it read in ELWS=123.441 and t2=7.00.
This only happened in a few places.
Is there some way to search for the entries where ELWS ended up with 3 decimal places? Also, all fields are double type.
SELECT * FROM someTable
WHERE (ELWS * 1000) % 10 != 0
SQLFiddle here
I have some strings in my database. Some of them have numeric values (but in string format of course). I am displaying those values ordered ascending.
So we know, for string values, 10 is greater than 2 for example, which is normal. I am asking if there is any solution to display 10 after 2, without changing the code or the database structure, only the data.
If for example I have to display values from 1 to 10, I will have:
1
10
2
3
4
5
6
7
8
9
What I would like to have is
1
2
3
4
5
6
7
8
9
10
Is there a possibility to ad an "invisible character or string which will be interpreted as greater than 9". If i put a10 instead of 10, the a10 will be at the end but is there any invisible or less visible character for that.
So, I repeat, I am not looking for a programming or database structure solution, but for a simple workaround.
You could try to cast the value as an number to then order by it:
select col
from yourtable
order by cast(col AS UNSIGNED)
See SQL Fiddle with demo
You could try appending the correct number of zeroes to the front of the data:
01
02
03
..
10
11
..
99
Since you have a mixture of numbers and letters in this column - even if not in a single row - what you're really trying to do is a Natural Sort. This is not something MySQL can do natively. There are some work arounds, however. The best I've come across are:
Sort by length then value.
SELECT
mixedColumn
FROM
tableName
ORDER BY
LENGTH(mixedColumn), mixedColumn;
For more examples see: http://www.copterlabs.com/blog/natural-sorting-in-mysql/
Use a secondary column to use as a sort key that would contain some sort of normalized data (i.e. only numbers or only letters).
CREATE TABLE tableName (mixedColumn varchar, sortColumn int);
INSERT INTO tableName VALUES ('1',1), ('2',2), ('10',3),
('a',4),('a1',5),('a2',6),('b1',7);
SELECT
mixedColumn
FROM
tableName
ORDER BY
sortColumn;
This could get difficult to maintain unless you can figure out a good way to handle the ordering.
Of course if you were able to go outside of the database you'd be able to use natural sort functions from various programming languages.
I am trying to create a query to take integer street address numbers and get the hundred block number for the address. E.g. any value from 300 to 399 yields 300 etc.
Is this possible in a single query or should I write a script?
you can use floor like this :
SELECT FLOOR(399/100)*100;
You can use:
SELECT number - MOD( number, 100 )