Mysql numbers compare in varchar fields - php

I need to compare numbers stored in varchar fields, for example i have a table:
id | values
1 | 2
2| 154
3 | 88
4 | 35
and I need to look for numbers, that are higher than 5, if I could use int attribute for values fields everything would be ok, but I have to use varchar. Is there any simple solution?

you need to use
CAST(`values` AS UNSIGNED)
like that
select * from table WHERE CAST(`values` AS UNSIGNED) > 5
DEMO
EDIT:
lets say you have signed number with negative sign -
then
select * from table1 WHERE CAST(`values` AS SIGNED) > 5
DEMO

Related

SQL Finding MAX number in a VARCHAR typed column

I have got SQL table where I have column DISTANCE (varchar50). It's number with dot.
+----+--------+--------+----------+
| ID | USERID | MONTH | DISTANCE |
+----+--------+--------+----------+
| 1 | 1 | 201707 | 7.25 |
+----+--------+--------+----------+
| 2 | 2 | 201707 | 9.17 |
+----+--------+--------+----------+
| 3 | 2 | 201707 | 10.31 |
+----+--------+--------+----------+
| 4 | 1 | 201706 | 10.08 |
+----+--------+--------+----------+
I would like to display MAX value od DISTANCE. If I ORDER BY distance DESC the result is:
SELECT * FROM mytable ORDER BY distance DESC
9.17
7.25
10.31
10.08
I try to find the MAX but still is not correct
SELECT MAX(distance) AS mvzd FROM mytable
9.17
How I can have correct max distance value which should be 10.31 with the provided sample data ?
PROBLEM
SOLUTION
You can use the following query :
select max(cast(distance as decimal(10,2))) AS mvzd FROM mytable
By your existing query, you are finding MAX on a VARCHAR typed column and you are getting correct result what you wrote! If you want to have your desired result, first you have to convert the type of that column like i did in the above query.
The problem is that you use wrong data type of the DISTANCE. In your case you store the distance values as varchar and that's why the ORDER BY and MAX() functions does not work as expected - in this case the ORDER BY is sorting values as a string from the very first character. In your example you tried to sorted in descending order to be like from lowest to larger - this will give you "unexpected" results if there will be value like 98.76 or 111.15.
All you need is to set the data type of DISTANCE column to float - a small number with a floating decimal point.
Change settings of your table to use float:
ALTER TABLE `mytable`
CHANGE COLUMN `distance` `distance`
FLOAT NULL DEFAULT NULL AFTER `month`;
Data stored as float will support all native functions such as MIN, MAX, ORDER BY etc. which will work as expected without any workarounds.
Here is corrected example using float: http://sqlfiddle.com/#!9/1f8692/1
Here is extended example using incorrect varchar(50): http://sqlfiddle.com/#!9/4da754e/1

MySQL - Add 0's to numbers with less than 9 digits

I have thousands of entries in my database, each with 9 digits or less. I would like to do a mass update, find all rows with digits less than 9 and add 0's to make them equal 9 digits.
For example, my table looks like:
ID | Number
---------------
0 | 489379
1 | 854744329
2 | 56456669
I would like to make it look like:
ID | Number
---------------
0 | 000489379
1 | 854744329
2 | 056456669
How would I do this with a MySQL query?
The lpad function should solve your issue:
SELECT `id`, LPAD(`Number`, 9, '0')
FROM mytable
To answer the question in the comment, this can also be applied in an update statement:
UPDATE mytable
SET `Number` = LPAD(`Number`, 9, '0')
WHERE LENGTH(`Number`) < 9
Use a case statement
update table
set column1 = case when length(column1) = 4 then concatenate('00000', column1)....
Have an element in the case statement for every possible length of the column. Kind of manual, and there is likely an easier way, but this is one possibility.

Lowest varchar amount

I am having issues comparing two values in the Mysql database and display the lowest one.
For instance I have this:
value1 = 23.4
value2 = 4.479
I tried this:
ORDER BY CAST(column AS SIGNED) ASC
but its not working
Again, field type is VARCHAR.
The result I am looking for should be 23.4 and not 4.479
any suggestions?
Simply put, you need to change your column to DECIMAL if you're storing prices. VARCHAR will be in alphabetical order. Read up on Numeric Types in MySQL.
ALTER TABLE your_table MODIFY COLUMN price DECIMAL(10,3);
A pretty costly work-around for you is to cast all values to decimal before sorting:
SELECT name, price FROM test ORDER BY CAST(price AS DECIMAL(20, 10)) ASC LIMIT 1
Just make sure your DECIMAL limits are enough to correctly cast all values in the column.
It will work if you CAST it to DECIMAL.
select * from tablename order by cast(price as decimal);
+--------+-------+
| name | price |
+--------+-------+
| nail | 0.95 |
| glue | 9.23 |
| hammer | 45.12 |
+--------+-------+

SELECT users from MySQL database by privileges bitmask?

I have users table and want to SELECT some rows by bitmask criteria. I'll try to explain my problem with small example.
Structure of table users
user_id int [primary key, auto_increment]
user_email varchar(200)
user_privileges int
Note: It has more fields but they are irrelevant for this question.
Filled table may look like this
+---------+--------------------+-----------------+
| user_id | user_email | user_privileges | << binary
+---------+--------------------+-----------------+
| 1 | john#example.com | 165 | 10100101
| 2 | max#example.com | 13 | 00001101
| 3 | trevor#example.com | 33 | 00100001
| 4 | paul#example.com | 8 | 00001000
| 5 | rashid#example.com | 5 | 00000101
+---------+--------------------+-----------------+
Now I want to SELECT users by specific privileges bitmask (by user_privileges column).
For example:
bitmask=1 [00000001] would select user-ids 1, 2, 3 and 5
bitmask=9 [00001001] would select user-id 2 only
bitmask=5 [00000101] would select user-ids 1, 2 and 5
bitmask=130 [10000010] would select none
My question: Is it possible from query or I have to go all users one-by-one and check this value from PHP code? Also, is it possible if field user_privileges is text, containing hexadecimal numbers, instead of integers? I need working mysql query example.
Note: This is just a simple example with 8-bit privilege-set. In real environment it may have larger sets (greater integers, more bytes). Creating separate column for each privilege state works fine, but that's not possible solution. I'd rather work with hex values, but integers are fine too, something is better than nothing.
Thanx in advance.
SELECT
*
FROM
users
WHERE
(user_privileges & <level>) = <level>
<level> being the access level you want to search on (e.g. 1, 5, 9, 130, etc.)
[...] want to SELECT some fields
Wrong. You want to select some Rows. Columns are usually called fields.
You are supposed to read the Documentation: Bit Functions are documented for mysql.
So you can try:
Select * from users WHERE (user_privileges & 1) >0

Using random in php mysql can't random id?

I have a sample code
products(id, name)
1 | Apple
2 | Sony
3 | Nokia
4 | Samsung
5 | LG
6 | Motorola
7 | Ekricson
And mysql:
SELECT id, name FROM `products` AS prod
ORDER BY RAND(prod.id) LIMIT 5
When i run code is result is:
4 | ...
7 | ...
1 | ...
5 | ...
6 | ...
But next ... is result is:
4 | ...
7 | ...
1 | ...
5 | ...
6 | ...
Id not change when run random, how to fix ix
You should use ORDER BY RAND() and not ORDER BY RAND(prod.product_id)
From RAND manual:
RAND(), RAND(N)
Returns a random floating-point value v in the range 0 <= v < 1.0.
If a constant integer argument N is specified, it is used as the seed value
which produces a repeatable sequence of column values.
The argument inside the RAND(x) function is the seed, so you're seeding it with the same value every time. Instead, leave it as RAND() and it will be different.
SELECT id, name FROM products AS prod
ORDER BY RAND() LIMIT 5
You probably want just ORDER BY RAND() rather than ORDER BY RAND(prod.id) -- if you give RAND() an integer argument, it's used as the seed value, so you will always get the same "random" value back.
See http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand for the details.
Use this way:
SELECT id, name FROM `products` AS prod
ORDER BY RAND() LIMIT 5;
See manual here

Categories