BETWEEN operator working wrong in MySQL - 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

Related

Why is my query returning "OK" instead of rows?

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)

MYSQL Rounding issue

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.

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;

Order a VARCHAR column numerically

Current SQL: SELECT code FROM myTable ORDER BY code ASC
code
---
11
113
12
13A
This is the current order I have of a MySQL table.
I want the order to be A-Z, 1-10 however, numerically, like this:
code
---
11
12
13A
113
The reason I cannot achieve this effect in the first place is because the code column is varchar and not int. However as shown in the example, some codes have a letter prepended to them so I cannot change this to integer.
How can I get around this problem without changing the data type?
The simplest way is to use silent conversion. Just add 0:
order by code + 0
In practice, you might want:
order by code + 0, code
This should work, sorting by number first and alpha after (if the same number):
select * from myTable
order by cast(replace(code,'[0-9]+','') as unsigned), code
See this SQL Fiddle

SQL/Sybase query group by a substring of a column value

I am making a simple select query with the following result -
select source_uri from image
--
source_uri
"image/30022/A.jpg"
"image/30022/B.jpg"
"image/30022/C.jpg"
"image/30022/D.jpg"
"image/30023/A.jpg"
"image/30023/B.jpg"
"image/30023/C.jpg"
"image/30023/D.jpg"
"image/30024/A.jpg"
"image/30024/B.jpg"
"image/30024/C.jpg"
"image/30024/D.jpg"
I want result like -
source_uri
"image/30022/A.jpg"
"image/30023/B.jpg"
"image/30024/C.jpg"
I tried having a group by clause... but it doesnot group by since the values are different.
--
I am able to use the SUBSTRING_INDEX function in mySQL.
Looking for the exact alternative in Sybase.
If there is always the last 6 i.e D.jpg" characters you need to apply group by then you can use the RIGHT(col,len),Right returns the rightmost len characters from the string str, or NULL if any argument is NULL
select source_uri
from image
GROUP BY RIGHT(source_uri ,6)
RIGHT(str,len)
Other way you need get the part after last / so use SUBSTRING_INDEX
select source_uri
from image
GROUP BY SUBSTRING_INDEX(source_uri ,'/',-1)
SUBSTRING_INDEX(str,delim,count)
EDIT after reading comments
SUBSTRING_INDEX(source_uri ,'/',-1) /* will give you A.jpg*/
Now to get remaining part you can do so
SUBSTRING_INDEX(source_uri ,SUBSTRING_INDEX(source_uri ,'/',-1) ,1) /* will give you image/30022/ */
FOR MySQL -
M Khalid Junaid's answer works great.
FOR Sybase -
I did a lean trick.. not sure how efficient this is -
select source_uri from image
Problem: To extract "image/30022" from "image/30022/a.jpg"
Solution:
1) Got the file length - file_length = CHARINDEX('/', reverse(source_uri))
2) Substring - SUBSTRING(source_uri, 1, LEN(source_uri) - file_length)
select SUBSTRING(source_uri, 1, LEN(source_uri) - file_length) from image
group by SUBSTRING(source_uri, 1, LEN(source_uri) - file_length)