WHY IS THIS HAPPENING?
SELECT
c.tax_rate,
c.line_item_total_price,
c.shipping_total_price,
ROUND((c.tax_rate *
(c.line_item_total_price+c.shipping_total_price)),2),
(c.tax_rate * (c.line_item_total_price+c.shipping_total_price))
FROM carts c
WHERE c.id = 323002;
returns:
.07
925.00
62.50
69.12
69.125
But this:
SELECT
ROUND((.07 * (925.00+62.50)),2),
(.07 * (925.00+62.50));
Returns the correct:
69.13
69.1250
Why the extra 0 at the end of that one?
FYI: Shipping and line item are DECIMAL(10,2) and tax is DOUBLE
I don't know a lot about MySQL but I would guess that it's an implicit conversion thing based on the column types. Maybe try casting everything until you find out what one was giving you the problem. DECIMAL(10,2) only allows 2 decimal places (that's what the 2 is)
Try
SELECT 1.00 * 1.0
You will get 1.000. This is because the exact result for DECIMAL(a,b) * DECIMAL(c,d) needs a DECIMAL(a+c,b+d) as data type.
Related
I have the following query:
SELECT
(sign(mr.p1_h2h_win_one_time - mr.p2_h2h_win_one_time)) AS h2h_win_one_time_1,
(abs(mr.p1_h2h_win_one_time - mr.p2_h2h_win_one_time) ^ 2) AS h2h_win_one_time_2
FROM belgarath.match_result AS mr
LIMIT 10
Which returns:
However, when I try to multiply the two fields:
SELECT
(
sign(mr.p1_h2h_win_one_time - mr.p2_h2h_win_one_time)
) *
(
abs(mr.p1_h2h_win_one_time - mr.p2_h2h_win_one_time) ^ 2
) AS h2h_win_one_time_comb
FROM belgarath.match_result AS mr
LIMIT 10
Workbench simply returns OK instead of any rows.
Doing some investigation I can get the first two rows to display if I use LIMIT 2. Looking at the returned values above I guess there must be some issue with multiplying the minus values or zero values from rows 3-10. However, this can be done simply on a calculator so what am I missing?
Maybe you think that the operator ^ is the power operator when in fact it is the Bitwise XOR operator.
MySql has the function pow() for your case:
pow(abs(mr.p1_h2h_win_one_time - mr.p2_h2h_win_one_time), 2)
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
I have this in a mysql table:
id and bolag_id are int. lat and lngitude are double.
If I use the the lngitude column, no results are returned:
lngitude Query: SELECT * FROM location_forslag WHERElngitude= 13.8461208
However, if I use the lat column, it does return results:
lat Query: SELECT * FROM location_forslag WHERElat= 58.3902782
What is the problem with the lngitude column?
It is not generally a good idea to compare floating point numbers with = equals operator.
Is it correct to compare two rounded floating point numbers using the == operator?
Dealing with accuracy problems in floating-point numbers
For your application, you need to consider how close you want the answer to be.
1 degree is about 112km, and 0.00001 degrees is about 1.1 metres (at the equator). Do you really want your application to say "not equal" if two points are different by 0.00000001 degrees = 1mm?
set #EPSLION = 0.00001 /* 1.1 metres at equator */
SELECT * FROM location_forslag
WHERE `lngitude` >= 13.8461208 -#EPSILON
AND `lngitude` <= 13.8461208 + #EPSILON
This will return points where lngitude is within #epsilon degrees of the desired value.
You should choose a value for epsilon which is appropriate to your application.
Floating points are irritating....
WHERE ABS(lngitude - 13.8461208) < 0.00000005
Convert float to decimal for compare. I had the same problem and solved like this:
SELECT
[dbo].[Story].[Longitude],
[dbo].[Story].[Latitude],
[dbo].[Story].[Location],
FROM
[dbo].[Story],
[dbo].[Places]
WHERE
convert(decimal, [dbo].[Story].[Latitude]) = convert(decimal, [dbo].[Places].[Latitude])
and
convert(decimal, [dbo].[Story].[Longitude]) = convert(decimal, [dbo].[Places].[Longitude])
and
[dbo].[Places].[Id] = #PlacesID
and
[dbo].[Story].IsDraft = 0
ORDER BY
[dbo].[Story].[Time] desc
Look at the first 3 rows after the WHERE clausule.
Hope it helps.
i have a problem during count data use this query:
SELECT A.*,
COUNT( B.Serial_number ) AS Qty_insp,
CONCAT(ROUND(A.`Reject_qty`/ COUNT(B.Serial_number)*100, 2),'%') AS NG_Ratio
FROM oqc_defect A
LEFT JOIN inspection_report B ON A.Model = B.Model
AND A.Line = B.Line
GROUP BY A.Problem_date
i get result as encryption code for NG_Ratio like : 3532e......
why its happen, how to resolve this problem?
Edit
Reject_qty Qty_insp NG_Ratio
2 20 10%
Why it's happening: probably it's outputting exponential formatted numbers, like 3532e-2 being equivalent to 35.32.
It's a little hard to tell since you cut off the output at the e, just as it was getting interesting :-)
I think cast() with decimal format may be able to turn that into the desired format or, alternatively, try:
ROUND (A.`Reject_qty` * 100) / COUNT (B.Serial_number), 2)
(I haven't tested this, it may not work).
CONCAT(CAST(ROUND(A.`Reject_qty`/ COUNT(B.Serial_number)*100, 2) AS CHAR),'%') AS NG_Ratio
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.