Generate 6 digit random number in mysql - mysql

I want to generate random 6 digit number in mysql but sometime it generate only 5 digit.
UPDATE member SET updates = FLOOR(RAND() * 999999)

If the problem is that you are missing leading zeros, you can left pad with spaces:
UPDATE member
SET updates = LPAD(FLOOR(RAND() * 999999.99), 6, '0');
I hope you understand that "random" means "random" and different rows can get the same value.

This generate 6 digit number exactly:
UPDATE member SET updates = FLOOR((RAND() * (999999-100000+1))+100000);

You can generate random 6 digit number in mysql
UPDATE member SET updates = LPAD(FLOOR(RAND() * 999999.99), 6, '0');
+--------+
| Value |
+--------+
| 499540 |
| 550607 |
| 254419 |
| 620272 |
| 338104 |
| 829705 |
+--------+
or you can also random number generate in mysql 1 to 200 in increment form. you can change in place of 200 to **** as you like
UPDATE member SET updates = FLOOR(1 + (RAND() * 200));
+--------+
| Value |
+--------+
| 1 |
| 180 |
| 50 |
| 85 |
| 33 |
| 165 |
+--------+

Related

Get count of zeros in an integer using MySQL

Let's say I have an integer value in MySQL (10090). I need to count all occurrences of the zero digit in that number. So for the previous case it would return 3:
select count_zeros(number) from dual;
-- when number = 10090, it return 3
-- when number = 10000, it return 4
How can I do that the fastest way using a MySQL query?
You can compare the string length with and without the character you want to count.
Solution using LENGTH
-- 0 in 10090: 3
-- 0 in 10000: 4
SELECT
(LENGTH(number) - LENGTH(REPLACE(number, '0', ''))) AS char_count
FROM dual;
A better and safer solution is to use the CHAR_LENGTH function instead of the LENGTH function. With CHAR_LENGTH function you can also count multi-byte characters (like §).
Solution using CHAR_LENGTH
-- § in 100§0: 1
SELECT
(CHAR_LENGTH(number) - CHAR_LENGTH(REPLACE(number, '§', ''))) AS char_count
FROM dual;
You can also extend the above solution to count for a string value using multiple characters.
-- 12 in 10120012: 2
SELECT number,
FLOOR((CHAR_LENGTH(number) - CHAR_LENGTH(REPLACE(number, '12', ''))) / CHAR_LENGTH('12')) AS str_count
FROM dual;
demo on dbfiddle.uk
On MySQL you can create a function to use the above logic on a simpler way:
CREATE FUNCTION GetStringCount(strValue VARCHAR(255), strSearchValue VARCHAR(255))
RETURNS INT DETERMINISTIC NO SQL
RETURN FLOOR((CHAR_LENGTH(strValue) - CHAR_LENGTH(REPLACE(strValue, strSearchValue, ''))) / CHAR_LENGTH(strSearchValue));
You can use this new function GetStringCount like this:
-- example to count non-multi-byte character (here 0).
-- 0 in 10090: 3
-- 0 in 10000: 4
SELECT number, GetStringCount(number, '0') AS strCount
FROM dual;
-- example to count multi-byte character (here §).
-- § in 100§0: 1
SELECT number, GetStringCount(number, '§') AS strCount
FROM dual;
-- example to count a string with multiple characters.
-- 12 in 10120012: 2
SELECT number, GetStringCount(number, '12') AS strCount
FROM dual;
I think the first thing to be done is, casting those integer values to string.
https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast
Then find occurences of a certain char
https://lists.mysql.com/mysql/215049
mysql> create table numbers(x int);
Query OK, 0 rows affected (0,38 sec)
mysql> select * from numbers;
+-----------+
| x |
+-----------+
| 123000 |
| 1300 |
| 135600 |
| 135623400 |
| 13560 |
| 135160 |
| 13514560 |
| 1351120 |
| 13512310 |
+-----------+
9 rows in set (0,00 sec)
Find occurences of zero
mysql> select x, round((length(cast(x as char(11))) - length( replace( cast( x as char(11) ), "0", "" ) ))/length("0")) as str_x from numbers limit 5;
+-----------+-------+
| x | str_x |
+-----------+-------+
| 123000 | 3 |
| 1300 | 2 |
| 135600 | 2 |
| 135623400 | 2 |
| 13560 | 1 |
+-----------+-------+
5 rows in set (0,00 sec)
Find thirteens
mysql> select x, round((length(cast(x as char(11))) - length( replace( cast( x as char(11) ), "13", "" ) ))/length("13")) as str_x from numbers;
+-----------+-------+
| x | str_x |
+-----------+-------+
| 123000 | 0 |
| 1300 | 1 |
| 135600 | 1 |
| 135623400 | 1 |
| 13560 | 1 |
| 135160 | 1 |
| 13514560 | 1 |
| 1351120 | 1 |
| 13512310 | 1 |
| 132134534 | 2 |
+-----------+-------+
10 rows in set (0,00 sec)
mysql>

How to convert binary to decimal(39,0) in MySQL?

I have ipv6 addresses stored in decimal(39,0) unsigned format.
I need to query the database. I'm using INET6_ATON() which converts ipv6 to binary. Now I need to convert that binary to decimal(39,0).
Is this possible in pure MySQL?
Yes, is possible. Because DECIMAL representation of IPv6 is bigger than maximum 64-bit integer (18,446,744,073,709,551,615), you must split the 'string' up to 16 characters and then to join them together:
example table:
mysql> select * from ip_table;
+----+-----------------------------------------+
| id | ipv6 |
+----+-----------------------------------------+
| 1 | 2001:db8:a0b:12f0::1 |
| 2 | 3731:54:65fe:2::a7 |
| 3 | FE80:0000:0000:0000:0202:B3FF:FE1E:8329 |
| 4 | FE80::0202:B3FF:FE1E:8329 |
+----+-----------------------------------------+
4 rows in set
SQL statement:
select id,
(
cast(conv(substr(HEX(INET6_ATON(t.`ipv6`)), 1, 16), 16, 10) as decimal(65))*18446744073709551616 +
cast(conv(substr(HEX(INET6_ATON(t.`ipv6`)), 17, 16), 16, 10) as decimal(65))
) as converted
from ip_table t
result:
+----+-----------------------------------------+
| id | converted |
+----+-----------------------------------------+
| 1 | 42540766414390830568948465903729639425 |
| 2 | 73361969000969283948743196392239923367 |
| 3 | 338288524927261089654163772891438416681 |
| 4 | 338288524927261089654163772891438416681 |
+----+-----------------------------------------+
4 rows in set
Note that last two IPs are the same, I've put them in different format in order to test is the script works properly.

Approximate selection between decimal values in perl-DBI

I was developed database table by using this query
create table test(
id varchar(10),
alpha varchar(1000),
marks decimal(10,4)
);
In the marks should use decimal datatypes.
marks column
1100.2523
1100.2722
733.8375
1192.257
587.6248
392.0859
I should use to select query
select * from test where marks="1100.2625";
and
select * from test where marks="392.0252";
The output is Empty set (0.00 sec)
Expected Output is
1100.2523 1100.2722 and 392.0859 this rows.
If possible to select between decimal range.?
This is usually not possible using the query and for that you need to define a benchmark for the approximation.
Now if you have a bench mark set for the approximation and lets say its something as
If the marks and compared value is same rounding to first decimal point then its a match.
If the after rounding to first decimal point both marks and input and taking the difference the value is 1 or -1 then its a match.
Using the above benchmark it could done.
Consider the following table
mysql> select * from test ;
+------+-------+-----------+
| id | alpha | marks |
+------+-------+-----------+
| 1 | aa | 1100.2523 |
| 2 | bb | 1100.2722 |
| 3 | cc | 392.0859 |
+------+-------+-----------+
mysql> select * from test where round(marks,1) = round(1100.2625,1);
+------+-------+-----------+
| id | alpha | marks |
+------+-------+-----------+
| 1 | aa | 1100.2523 |
| 2 | bb | 1100.2722 |
+------+-------+-----------+
mysql> select * from test
where round(marks,1) = round(392.0252,1)
or round(marks,1) - round(392.0252,1) = 0.1
or round(marks,1) - round(392.0252,1) = -0.1;
+------+-------+----------+
| id | alpha | marks |
+------+-------+----------+
| 3 | cc | 392.0859 |
+------+-------+----------+

How to make two updates in one query in MySQL?

Having a table like this one:
+----+-------+
| id | Value |
+----+-------+
| 1 | 1000 |
| 2 | 1500 |
| 3 | 1250 |
| 4 | 2000 |
| 5 | 1800 |
+----+-------+
How can I update the column "value" while adding, for exame, x% for those who are less than 1600 and y % for those greater than this value in one query? Is it possible?
I can't update the lowest first because eventually some value may exceed the range of 1600. That way, when I have updated the largest , could do it in values that had already received the increase .
You can use a case statement:
update table t
set value = value * (case when value < 1600 then 1 + x
when value > 1600 the 1 + y
else 1
end);

How do I compare average runtime of two functions in MySQL?

I wanted to compare average runtime of two functions in MySQL -
Square distance: pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2)
vs
Dot product: x1 * x2 + y1 * y2 + z1 * z2
Now, whichever function I choose is going to run around 50,000,000,000 times in a single query! So, even the tiniest of difference in their runtime matters.
So, I tried profiling. Here's what I got,
mysql> show profiles;
+----------+------------+-----------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------------------------------------------+
| 4 | 0.00014400 | select pow(rand()-rand(),2)+pow(rand()-rand(),2)+pow(rand()-rand(),2) |
| 5 | 0.00012800 | select pow(rand()-rand(),2)+pow(rand()-rand(),2)+pow(rand()-rand(),2) |
| 6 | 0.00017000 | select pow(rand()-rand(),2)+pow(rand()-rand(),2)+pow(rand()-rand(),2) |
| 7 | 0.00024800 | select pow(rand()-rand(),2)+pow(rand()-rand(),2)+pow(rand()-rand(),2) |
| 8 | 0.00014400 | select pow(rand()-rand(),2)+pow(rand()-rand(),2)+pow(rand()-rand(),2) |
| 9 | 0.00014000 | select pow(rand()-rand(),2)+pow(rand()-rand(),2)+pow(rand()-rand(),2) |
| 10 | 0.00014900 | select pow(rand()-rand(),2)+pow(rand()-rand(),2)+pow(rand()-rand(),2) |
| 11 | 0.00015000 | select rand()*rand()+rand()*rand()+rand()*rand() |
| 12 | 0.00012000 | select rand()*rand()+rand()*rand()+rand()*rand() |
| 13 | 0.00015200 | select rand()*rand()+rand()*rand()+rand()*rand() |
| 14 | 0.00022500 | select rand()*rand()+rand()*rand()+rand()*rand() |
| 15 | 0.00012700 | select rand()*rand()+rand()*rand()+rand()*rand() |
| 16 | 0.00013200 | select rand()*rand()+rand()*rand()+rand()*rand() |
| 17 | 0.00013400 | select rand()*rand()+rand()*rand()+rand()*rand() |
| 18 | 0.00013800 | select rand()*rand()+rand()*rand()+rand()*rand() |
+----------+------------+-----------------------------------------------------------------------+
15 rows in set, 1 warning (0.00 sec)
This is not very helpful at all, runtimes fluctuate around so much that I have no clue which one is faster and by how much.
I need to run each of these functions like 10,000 times to get a nice and consistent average runtime. How do I accomplish this in MySQL?
(Note that rand() is called 6 times in both the functions so it's runtime doesn't really make a difference)
Edit:
Sure, I can create a temp table, it would be slightly inconvenient, fill it with random values, which again is not straight forward (see How do I populate a mysql table with many random numbers) and then proceed to comparing my functions.
I wanted to know If a better way existed in MySQL.
In the best of the cases, the function pow detects that the exponent is the integer 2 and performs exponentiation with a single multiply. There is no reason it could beat a pure multiply.