mysql double values checking problem - mysql

I wanna make a query that fetches only the rows that has 'cost' value grader than zero.The cost column has double data type.When i write a query like that,
select cost from xxx where cost>0;
it retrieves the rows only that has value grader than or equal to one.For example it doesnt take like 0.02 or 0.3 values.The query sees these type values as zero.How can i achieve my goal?
Thanks for advance...

I can't replicate your problem using mysql 5.41.
Show us the result of describe table xxx;
What happens if you issue the query:
select cost from xxx where cost > 0.0;
Is your query actually:
select ceil(cost) from xxx where cost > 0.0;
If so, for values of cost > 0 but <= 1, you'd get a result set of 1.

Related

how to get average value from 2 colomn ini mysql

i have table = table_a
and filed =
val1,val2,val3 =(10,10,7)
i want get Avg value from val1,val2,val3 on mysql, i was try with AVG(val1,val2,val3) from table_a it's not work,
somebody can help me?
thanks
Since you want to get the average of the columns not rows, just perform basic calculation
(val1 + val2 + val3) / 3.0 AS Average

How to create query with simple formula?

Hey is there any way to create query with simple formula ?
I have a table data with two columns value_one and value_two both are decimal values. I want to select this rows where difference between value_one and value_two is grater then 5. How can i do this?
Can i do something like this ?
SELECT * FROM data WHERE (MAX(value_one, value_two) - MIN(value_one, value_two)) > 5
Example values
value_one, value_two
1,6
9,3
2,3
3,2
so analogical difs are: 5, 6, 1, 1 so the selected row would be only first and second.
Consider an example where smaller number is subtracted with a bigger number:
2 - 5 = -3
So, the result is a difference of two numbers with a negation sign.
Now, consider the reverse scenario, when bigger number is subtracted with the smaller number:
5 - 2 = 3
Pretty simple right.
Basically, the difference of two number remains same, if you just ignore the sign. This is in other words called absolute value of a number.
Now, the question arises how to find the absolute value in MySQL?
Answer to this is the built-in method of MySQL i.e. abs() function which returns an absolute value of a number.
ABS(X):
Returns the absolute value of X.
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32
Therefore, without worrying about finding min and max number, we can directly focus on the difference of two numbers and then, retrieving the absolute value of the result. Finally, check if it is greater than 5.
So, the final query becomes:
SELECT *
FROM data
WHERE abs(value_one - value_two) > 5;
You can also do complex operations once the absolute value is calculated like adding or dividing with the third value. Check the code below:
SELECT *
FROM
data
WHERE
(abs(value_one - value_two) / value_three) + value_four > 5;
You can also add multiple conditions using logical operators like AND, OR, NOT to do so. Click here for logical operators.
SELECT *
FROM
data
WHERE
((abs(value_one - value_two) / value_three) + value_four > 5)
AND (value_five != 0);
Here is the link with various functions available in MySQL:
https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
No, you would just use a simple where clause:
select *
from data
where abs(value_one - value_two) > 5;

Update anomaly. Mysql sql different rows affected count

SELECT * FROM `attempts` WHERE date = '27-04-2014' LIMIT 0 , 30
This particular query gave 386 results(PHPmyAdmin) but on executing the below query
UPDATE `attempts` SET points = points *2 WHERE date = '27-04-2014'
I got 379 rows affected. . Shouldn't I get same numbers? Any other reasons possible? Or am I wrong somewhere?
The query won't affect the rows where points = 0, because doubling the value of points won't have any effect.
For example, try running this query:
UPDATE `attempts` SET points = points + 0 WHERE date = '27-04-2014'
and it will show 0 rows affected.
Also, the count shown by phpMyAdmin is an estimate, if you're using InnoDB. Use COUNT(*) to get the exact count.
SELECT COUNT(*) FROM `attempts` WHERE date = '27-04-2014'
"Affected rows" counts only rows that were changed. If you have records with 0 points, doubling the number of points has no effect and these records will not be included in the count.

Sql selection using between operator

In my database table there are two fields named salary_to and salary_from.Now in front end of my website user can enter a salary in an input field say 20000.Now i wanna perform a check in the database that if any row have salaryto and salaryfrom between which the value '20000' lies.
My desired algo looks like something like this
SELECT all ROW where salaryto is less than 20000 and salaryfrom is greater than 20000
Can i use the between operator to perform this kind of filterring or i just ave to use >,< operator to this?
Provided that I'm using mysql database for my project.
The between operator is INCLUSIVE meaning it includes the START and END Value. While < > are exclusive.
SELECT *
FROM tableName
WHERE salaryto < 20000 and salaryfrom > 20000
So the rows that are fetched here are with salaryto value of 19999 and below AND salaryfrom value of 20001 and above, assuming that it increases by 1.
You can use this:
SELECT * FROM yourTable WHERE 2000 BETWEEN salary_to AND salary_from
But this is Recommended:
SELECT * FROM yourTable WHERE (salary_to < 2000) and (salary_from > 2000)

BETWEEN operator working wrong in MySQL

I have a query having BETWEEN operator but it showing wrong results
My query-
SELECT * FROM register WHERE height BETWEEN '1' AND '6'
It also shows the user with height 10, 12 and 16 which is wrong. What is the problem with this query?
I have another query which work fine but is not proper way of using as it make query lengthy
SELECT * FROM register WHERE height > 1 AND height < 12
Give me idea for right way of getting the query as if more condition is added it would be hard for the query to understand and code.
Assuming height is an integer should it not be
SELECT * FROM register WHERE height BETWEEN 1 AND 6
You don't need the single quotes
If:
1. You can't or don't want to change the column type
2. The charachters in the field are only numbers
You can change your query to:
SELECT *
FROM register
WHERE CONVERT(height, UNSIGNED INTEGER) BETWEEN 1 AND 6
See my example at this SQL fiddle.
Check the data type of your column 'Height' it should be a int or float or double and if it is amongs above run the following query
SELECT * FROM register WHERE height BETWEEN 1 AND 6
If the height field is not numeric, you could convert it before making you comparison.
SELECT * FROM register WHERE CONVERT(height, unsigned) BETWEEN 1 AND 6