I'm was trying to make function that will return random character. What I have is a string that contains alphanumerics. In Postgresql I can set text as array, but I don't know how MySQL do same. for example:
in postgresql
DECLARE chars TEXT[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
So when I want get chars[10] it will return A. How to do this in MySQL?
I think the questoin is not about arrays, it is about get random char, right? In this case You can use next approach in MySQL:
SELECT SUBSTRING(
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
ROUND(RAND()*61)+1,
1
) as random_char;
The SUBSTRING function returns one char from random position.
Try it on SQLize.online
Starting MySQL 5.7, one option is to use a JSON array:
set #chars = '["0", "1", "A", "B"]';
select json_unquote(json_extract(#chars, '$[2]')) as value_at_index_2;
Yields:
| value_at_index_2 |
| :--------------- |
| A |
Related
I have a MySQL JSON column containing values such as:
[33173,33386,24272,33499,33526,33347]
Would it be possible, with JSON functions, to cast each value in the array to string? The output would be:
["33173","33386","24272","33499","33526","33347"]
I'd like to avoid resorting to dirty REPLACE() calls.
If you use MySQL 8.0, you can use the JSON_TABLE() function to explode the array into rows, then cast them to CHAR(), then JSON_ARRAYAGG() to implode the rows back into a JSON array.
set #j = '[33173,33386,24272,33499,33526,33347]';
select json_arrayagg(cast(i as char(5))) as j
from json_table(#j, '$[*]' columns (i int path '$')) j;
Output:
+--------------------------------------------------------+
| j |
+--------------------------------------------------------+
| ["33173", "33386", "24272", "33499", "33526", "33347"] |
+--------------------------------------------------------+
I have a table that contains a JSON column, and in it a JSON array:
mysql> SELECT profile->'$.countriesVisited' from users;
+-------------------------------+
| profile->'$.countriesVisited' |
+-------------------------------+
| ["us", "il"] |
| ["co", "ph"] |
+-------------------------------+
2 rows in set (0.00 sec)
I want to convert the values inside the array into upper case. (I am assuming this answer would also assist lower case, string replacements.. etc.)
I've been trying to use UPPER, JSON_ARRAY, JSON_QUOTE, JSON_UNQUOTE, etc - at best I end up with a string representation of what I want.
How can I do this? I'm running MySQL 5.7.19.
You need to use JSON casting. Try the following:
UPDATE users
SET profile = JSON_SET(
profile,
'$.countriesVisited',
CAST(
UPPER(profile->'$.countriesVisited')
AS JSON
)
);
I'm storing permissions into DB with Array JSON String, and i want select them by permission specific permission. at this time I'm selecting them like this:
1 | Dog | [3,4]
2 | Cat | [33,4]
3 | Tiger | [5,33,4]
4 | wolf | [3,5]
SELECT * FROM `pages` WHERE access REGEXP '([^"])3([^"])'
it works but not as it should work. This query gives me all records which contains 3 but also it gives which contains 33. my question is how i must format my regexp to get row by specific value into json string.
p.s i have mysql 5.5 so as i know on this version json functions is not supported
If you only have numbers in the fields, you can alter your regexp to only take values where the string you are looking for (here the '3') does not have another number immediately close to it :
SELECT * FROM `pages` WHERE access REGEXP '([^"0-9])3([^"0-9])'
REGEXP '[[:<:]]3[[:>:]]'
That is, use the "word boundary" thingies.
When i run this code it returns me as expected all sites with the listed ids
SELECT * FROM `sites` WHERE id IN (142,150,40,42,21,99,162,110,121)
But this returns only the site with the id 142. Why does this even return something? I would have expected an error.
SELECT * FROM `sites` WHERE id IN ("142,150,40,42,21,99,162,110,121")
MySql (correctly) doesn't look inside the string to see that it contains something that looks like an in list.
It just sees an in list with a single item that is a string. You get an implicit cast from string to int to convert that single expression.
But instead of doing the sensible thing and raising an error because the string "142,150,40,42,21,99,162,110,121" is not remotely a valid integer it stops at the first non numeric character and so the string casts to 142.
So your query is evaluated as in (142)
try this:
select 1 + "142,150,40,42,21,99,162,110,121";
+---------------------------------------+
| 1 + "142,150,40,42,21,99,162,110,121" |
+---------------------------------------+
| 143 |
+---------------------------------------+
In the manual, it states that:
To cast a string to a numeric value in numeric context, you normally do not have to do anything other than to use the string value as though it were a number
If you use a string in an arithmetic operation, it is converted to a floating-point number during expression evaluation.
Your string is numified
MYSQL command:
UPDATE `tbl_objednavka` SET
`TOTAL` = '6300',
`EANS` = CAST('8433611369655;' AS char)+CAST(`EANS` AS char),
`COUNTS` = CAST('1;' AS char)+CAST(`COUNTS` AS char)
WHERE `ID_OBJEDNAVKA`=2;
_____________________________________
| | |
| EANS | COUNTS |
+-----------------+-----------------+
| | |
| 8433611364094 | 1 |
+-----------------+-----------------+
It for some weird reason '8433611369655;' dont merge strings but add one number to the other so I get something as this: 1.6867223e+13...
I need to get an array, so this: 8433611369655;8433611364094 in EANS and 1;1 in COUNTS
I can use php for this, but I would love to do this using SLQ only
You are using MySQL, so you want to use concat() rather than + for string concatenation.
Also, you should never convert values to char() without a length. In this case, though, I think varchar() would be a more appropriate type.
However, I would suggest this query:
UPDATE `tbl_objednavka`
SET `TOTAL` = '6300',
`EANS` = concat('8433611369655;', `EANS`),
`COUNTS` = concat('1;', `COUNTS`)
WHERE `ID_OBJEDNAVKA`=2;
You don't seem to need the casts at all. The types of EANS and COUNTS should be character to begin with, because you are assigning character values to them.
If they are numeric, then you need to alter the table so they can hold the values you want. In practice, I would suggest adding new columns in this case. Or, using a view to create the new columns.
UPDATE `tbl_objednavka` SET
`TOTAL` = '6300',
`EANS` = CONCAT('8433611369655;', CAST(`EANS` AS char)),
`COUNTS` = CONCAT('1;', CAST(`COUNTS` AS char))
WHERE `ID_OBJEDNAVKA`=2;
MySQL doesn't use + for string concatenation, you have to use the CONCAT() function.